【问题标题】:Trigger a function in a child directive from it's parent [angularJS]从它的父级触发子指令中的函数[angularJS]
【发布时间】:2014-12-30 01:28:39
【问题描述】:

所以当在子指令中使用指令属性require: '^ParentCtrl' 时,我总是完全反向执行此操作。然后使用 require 调用父函数;但是,我需要反过来做。

问题:
如何从父指令触发子指令中函数的执行。

注意: 1.子指令没有函数在link:里面 2. 本质上我想要一个反向需求。

父指令:

'use strict';
angular.module('carouselApp')
  .directive('waCarousel', function() {
    return {
        templateUrl: 'views/carousel/wa.carousel.html',
        controller: function($scope) {
            var self = this;
            // this function is being called based on how many pages there are
            self.carouselElLoaded = function(result) {
                var count = 1;
                Carousel.params.pageRenderedLength += count;
                //when all the pages are loaded
                if (Carousel.params.pageRenderedLength === Carousel.params.pageLength) {
                    Carousel.params.carouselReady = true;
                    // !!!!!!!! Trigger will go here!!!!!!!!!//
                    ChildCtrl.drawHotspots(); // (**for placement only**)
                } else {
                    Carousel.params.carouselReady = false;
                }

            };
        }
    }
})

儿童指令:

'use strict';
angular.module('carouselApp')
  .directive('waHotspots', function() {
    return {
        require: '^waCarousel',
        link: function (scope, element, attrs, ctrl) {
              //call this directive based on how
              scope.drawHotspots = function () {...};
        }
    })

【问题讨论】:

    标签: javascript angularjs angularjs-directive


    【解决方案1】:

    这可以通过让父控制器通过您创建的定义良好的 API 与子控制器对话来实现。这个想法是,您希望通过让每个各自的控制器尽可能少地了解彼此来保持父指令和子指令之间的松散耦合,但仍然有足够的知识来完成工作。

    要实现这一点,需要子指令中的父指令,并让子指令向父控制器注册自己:

    子指令:

      require: '^parentDirective',
      controller: function(){
           this.someFunc = function() {...}
     },
      link: function(scope,element,attr, parentCtrl){
          parentCtrl.register(element);
     }
    

    然后在你的parent指令中,实现register函数,并获取child的controller,在需要的时候调用child的函数:

    父指令:

      controller: function(){
          var childCtrl = undefined;
          this.register = function (element) {
              childCtrl = element.controller();
          }
         this.callChildFunc = function (){
                childCtrl.someFunc();
         }
    
      },
      link: function (scope,element){
            var ctrl = element.controller();
             ctrl.callChildFunc();
      }
    

    【讨论】:

    • 您说要松散耦合指令,但您的示例似乎非常紧密耦合。
    【解决方案2】:

    您总是可以通过 $watch 触发它。只需传入您要查看的父范围值并更改它的值。

    家长:

       $scope.drawHotspots = false;
    

    模板:

              waHotspots the-trigger="drawHotspots"....
    

    儿童指令:

          localTrigger: '@'    // Receive the value to watch
    
          scope.$watch('localTrigger',function() {
                  // call drawHotspots if value is set to true
          });
    

    【讨论】:

      【解决方案3】:

      这是一个老话题,但我今天来到这里,其他人也可能...
      我认为最好的方法是使用Service
      angular.module('App').service('SomeService', [SomeService]);

      然后将服务注入到父子节点...
      controller : ['$rootScope', '$scope','SomeService', SomeDirectiveController],

      使用该服务相互交谈...
      在他们的控制器SomeService.setParent(this)SomeService.setChild(this)

      服务将有一个字段来保存引用:

      this.parentCtrl = null;
      this.childCtrl = null;//or [] in-case you have multiple childs!
      

      在父母的某个地方:SomeService.childCtrl.someFunctionInChild()

      或者,如果您想要限制访问,请在服务中将字段设为私有:

      var parentCtrl = null;
      var childCtrl = null;//or [] in-case you have multiple childs of the same type!
      
      this.callUserFunc = function(param){childCtrl.someFunctionInChild(param)};
      

      在父母的某个地方:SomeService.callUserFunc(myparam)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-21
        • 1970-01-01
        • 2017-03-25
        • 1970-01-01
        • 2018-01-03
        • 2021-04-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多