javascript - AngularJS parent directive communicate with child directive - Stack Overflow

Consider two nested directives with isolate scopes:<dctv1><dctv2><dctv2><dctv1&g

Consider two nested directives with isolate scopes:

<dctv1>
    <dctv2></dctv2>
<dctv1>

If I want dctv2 to talk to dctv1 I have may options:

  1. I may require the controller of dctv1 in the definition of dctv2 using the require:'^dctv1'
  2. I may call an expression on the parent scope with the wrapper <dctv2 callParent="hello()"></dctv2> and scope:{callParent:'&'}
  3. I can also use $scope.$emit in dctv2 but then all parent scopes will hear the message.

Now I want dctv1 to talk to dctv2.

  1. The only way I may acplish this is to use $scope.$broadcast, but then all children will hear.

By talk to here i mean call a function or similar. Don't want to set up watches clogging the digestloop.

How can I make dctv1 notify dctv2 in the best way, making them loose-coupled? I should just be able to remove dctv2 without errors.

Consider two nested directives with isolate scopes:

<dctv1>
    <dctv2></dctv2>
<dctv1>

If I want dctv2 to talk to dctv1 I have may options:

  1. I may require the controller of dctv1 in the definition of dctv2 using the require:'^dctv1'
  2. I may call an expression on the parent scope with the wrapper <dctv2 callParent="hello()"></dctv2> and scope:{callParent:'&'}
  3. I can also use $scope.$emit in dctv2 but then all parent scopes will hear the message.

Now I want dctv1 to talk to dctv2.

  1. The only way I may acplish this is to use $scope.$broadcast, but then all children will hear.

By talk to here i mean call a function or similar. Don't want to set up watches clogging the digestloop.

How can I make dctv1 notify dctv2 in the best way, making them loose-coupled? I should just be able to remove dctv2 without errors.

Share Improve this question asked Feb 12, 2015 at 13:05 user1506145user1506145 5,29612 gold badges48 silver badges77 bronze badges 1
  • If you chose the events. You can call preventDefault and stopPropagation. In fact $event is an instance of jQuery event. – Raulucco Commented Feb 12, 2015 at 13:39
Add a ment  | 

4 Answers 4

Reset to default 3

Take a look at AngularJS NgModelController for some ideas.

Each <dctv2> directive would require <dvtv1> to have it's controller injected. You can then add objects or callbacks to properties of that controller, and remove them when <dctv2> is destroyed.

<dvtv1> would not talk directly to children, but would trigger callbacks bound to it's properties.

For example;

NgModelController has $parsers and $formatters that are an array of function callbacks. You push your own functions into the array to extend that controllers behavior.

When NgModelController performs input validation it's basically talking to other directives via these properties.

I would suggest using angular services. That way you can decouple your behavior into one or more services.

Take a look at this also : AngularJS : How to watch service variables?

One way is to make a Service/Factory that will municate with the controllers that you want.

For example, here's a getter/setter Factory

.factory('factoryName', function () {

    var something = "Hello";

    return {

        get: function () {
            return something;
        },

        set: function (keyword) {
            something = keyword;
            return something ;
        }
    };

}])

And then in your controllers:

.controller('controllerOne', ['factoryName', function (factoryName) {
    $scope.test = factoryName.get();
}]);

.controller('controllerTwo', ['factoryName', function (factoryName) {
    $scope.test = factoryName.get();
    $scope.clickThis = function (keyword) {
        factoryName.set(keyword);
    };
}]);

I suggest reading up on this : Can one controller call another?

You can manage it using an id for each child that have to be passed to the parent; the parent will broadcast back the event using that id: the child will do the action only if the id passed from the parent is the his own.

Bye

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348739a4623697.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信