Javascript 如何调用指令';从控制器到角度控制器的s函数

Javascript 如何调用指令';从控制器到角度控制器的s函数,javascript,angularjs,Javascript,Angularjs,我有一个指令控制一个个性化的multiselect。有时,我想从主控制器清除所有多选项。我用multiselect值填充了一个“过滤器”双向变量,我可以从中删除内容,但在这样做时,我还必须更改一些样式和其他内容。换句话说:我必须从属于控制器的按钮调用属于指令的方法。对于这种数据结构,这是可能的吗 (顺便说一句,我发现了其他问题和例子,但它们的指令没有自己的范围。) 我认为从控制器链接到指令的更好解决方案是: // in directive return { scope: {

我有一个指令控制一个个性化的multiselect。有时,我想从主控制器清除所有多选项。我用multiselect值填充了一个“
过滤器
”双向变量,我可以从中删除内容,但在这样做时,我还必须更改一些样式和其他内容。换句话说:我必须从属于控制器的按钮调用属于指令的方法。对于这种数据结构,这是可能的吗

(顺便说一句,我发现了其他问题和例子,但它们的指令没有自己的范围。)


我认为从控制器链接到指令的更好解决方案是:

// in directive

   return {
      scope: {
                controller: "=",         
             },
      controller: function($scope){
         $scope.mode = $scope.controller.mode;
         $scope.controller.function_i_need_to_call = function(){}
         $scope.controller.currentState = state;
      }
   }

// in controller
function testCtrl($scope){

     // config directive
     $scope.multiselectDirectiveController = {
           mode: 'test',
     };

     // call directive methods
     $scope.multiselectDirectiveController.function_i_need_to_call();

     // get directive property
     $scope.multiselectDirectiveController.currentState;
}

// in template
<Multiselect-directive controller="multiselectDirectiveController"></Multiselect-directive>
//in指令
返回{
范围:{
控制器:“=”,
},
控制器:功能($scope){
$scope.mode=$scope.controller.mode;
$scope.controller.function\i\u需要调用=function(){}
$scope.controller.currentState=状态;
}
}
//内部控制器
函数testCtrl($scope){
//配置指令
$scope.multiselectDirectiveController={
模式:“测试”,
};
//调用指令方法
$scope.multiselectDirectiveController.function_i_need_to_call();
//获取指令属性
$scope.multiselectDirectiveController.currentState;
}
//模板中

最佳解决方案和角度方式-使用
事件

现场示范

角度模块('ExampleApp',[]) .controller('ExampleOneController',函数($scope){ $scope.raise=函数(val){ $scope.$broadcast('raise.event',val); }; }) .controller('ExampleTowController',函数($scope){ $scope.raise=函数(val){ $scope.$broadcast('raise.event',val); }; }) .directive('simple',function(){ 返回{ 限制:“A”, 范围:{ }, 链接:功能(范围){ 作用域:$on('raise.event',函数(event,val){ log('i'm from'+val); }); } } });

示例一控制器
提高1
示例2控制器
提高2

在指令内定义为$parent scope函数,例如:scope.$parent.function\u i\u need\u to\u call=function(){..},然后在控制器中使用$scope.function\u i\u need\u to\u call()调用相同的方法您可以
scope.$watch
筛选
然后根据当前值调用函数。@saikumar但是如果指令在多个元素中工作怎么办?然后做一件事。scope.$on('function_i_need_to_call',function(){})在指令内,然后$broadcast如果有多个指令实例,这些方法将重写,我担心一个指令的多个实例会以这种方式将不同的$scope.multiselectDirectiveController变量传递给指令,而不会发生重写,例如:$scope.userMultiselectController、$scope.vehicle multiselectcontroller最佳解决方案和角度方式-使用事件。下面的答案是最简单的解决方案而不是最好的解决方案,如果您想调用一个指令的多个方法,例如map指令,那么您会遇到许多性能问题。所以广播不是解决方案,它是最简单的解决方案
// in directive

   return {
      scope: {
                controller: "=",         
             },
      controller: function($scope){
         $scope.mode = $scope.controller.mode;
         $scope.controller.function_i_need_to_call = function(){}
         $scope.controller.currentState = state;
      }
   }

// in controller
function testCtrl($scope){

     // config directive
     $scope.multiselectDirectiveController = {
           mode: 'test',
     };

     // call directive methods
     $scope.multiselectDirectiveController.function_i_need_to_call();

     // get directive property
     $scope.multiselectDirectiveController.currentState;
}

// in template
<Multiselect-directive controller="multiselectDirectiveController"></Multiselect-directive>