【问题标题】:What's the best way to call function within directive from controller从控制器的指令中调用函数的最佳方法是什么
【发布时间】:2014-11-16 16:37:39
【问题描述】:

只是想知道从控制器到指令函数的最佳通信方式是什么,我在其中一个按钮上单击了 ng-click,但该函数位于指令中,有没有一种方法可以在控制器中调用该函数(位于指令中)。我知道你可以使用作用域进行双重绑定,有没有更好的方法?

干杯

   app.controller('leadsListing', ['$scope', function($scope){
    $scope.filterresultcount = 0;
    $scope.records = [];
    $scope.filtertotal = '';

    $scope.$watch('filtertotal', function(){
        $scope.filterresultcount = parseInt($scope.filtertotal / 20);
    });

    $scope.moreFilterResult = function(){
        if($scope.filterresultcount > 0){
            $scope.filterresultcount--;
        }
        $scope.heyJoe(); // It's in diretive
    };
  }]);

app.directive('recordfilter', ['$http', 'filterService', function($http, filterService){
    return {
        scope: {
            names : '@names',
            model : '@model',
            records : '=records',
            filtertotal : '=filtertotal',
            filterresultcount : '=filterresultcount'
        },
        restrict: 'A', 
        replace: true,
        link: function($scope, iElm, iAttrs, controller) {
            $scope.heyJoe()
        }
    }
}

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    我相信实现这种控制器-->指令通信的最佳方式是使用控制器中的$scope.$broadcast,以及指令的控制器/链接函数中的$scope.$on。

    控制器:

    app.controller('leadsListing', ['$scope', function($scope){
      // ...
    
      $scope.moreFilterResult = function(){
          if($scope.filterresultcount > 0){
              $scope.filterresultcount--;
          }
          $scope.$broadcast('joeCalled'); 
      };
    }]);
    

    指令:

    app.directive('recordfilter', ['$http', 'filterService', function($http, filterService){
        return {
            scope: {
                names : '@names',
                model : '@model',
                records : '=records',
                filtertotal : '=filtertotal',
                filterresultcount : '=filterresultcount'
            },
            restrict: 'A', 
            replace: true,
            link: function(scope, iElm, iAttrs, controller) {
                scope.$on('joeCalled', function(){ 
                    // Do something...
                });
            });
        };
    }
    

    编辑:

    创建了此技术的工作示例: http://jsfiddle.net/9p3eyy5h/2/

    【讨论】:

    • 听起来不错,它比使用示波器更好,如果有更好的答案,我会投票并等待。
    • 我想如果你想做一些更接近你要求的事情,你可以在控制器的范围内创建一个空对象,使用'='数据绑定将它传递给指令,并附加一个函数指令中的它,以后可以由控制器调用:jsfiddle.net/9pm3zg5s/1 但是,我建议不要这样做,因为它在控制器和指令之间创建了显式依赖关系,如果指令丢失或更改,则会导致错误行为。
    • 请同时填写您的第二个答案,我将标记为赞成票 2
    • 谢谢@bluebill1049 :) 我将其添加为另一个答案。
    • 非常感谢!我花了三个小时试图弄清楚如何从控制器调用指令范围内的函数。谢谢谢谢!
    【解决方案2】:

    可以通过在控制器范围内放置一个空对象,使用 '=' 将其绑定到指令的范围,并在指令的链接函数/控制器中将函数附加到它来完成从控制器直接在指令中调用函数,稍后可以由包装控制器调用。

    控制器:

    app.controller('leadsListing', ['$scope', function($scope){
      // ...
    
      $scope.directiveFuncs = {};
      $scope.moreFilterResult = function(){
          if($scope.filterresultcount > 0){
            $scope.filterresultcount--;
          }
        $scope.directiveFuncs.heyJoe();
      };
    }]);
    

    指令:

    app.directive('recordfilter', ['$http', 'filterService', function($http, filterService){
      return {
        scope: {
            names : '@names',
            model : '@model',
            records : '=records',
            filtertotal : '=filtertotal',
            filterresultcount : '=filterresultcount',
    
            // Binding to the controller's func obj
            funcs: '='
          },
          restrict: 'A', 
          replace: true,
          link: function(scope, iElm, iAttrs, controller) {
            scope.funcs.heyJoe = function(){
              // Do something...
            }
        });
      };
    }
    

    HTML:

    <div ng-controller="leadsListing">
      <div recordfilter funcs="directiveFuncs"></div>
    </div>
    

    然而,我建议使用我的其他方法,因为它可以防止控制器和指令之间的直接依赖关系,因此更加健壮,因此如果指令丢失或更改,它不会引发错误。

    工作示例:
    http://jsfiddle.net/9pm3zg5s/1

    【讨论】:

    • 感谢 naor 两种方法都很棒,我实现了第一种,但其他人可能会在某些情况下发现它很常见。
    猜你喜欢
    • 2011-12-17
    • 2013-04-19
    • 2016-09-19
    • 2012-04-09
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多