2013-04-21 62 views
19

指令如何从具有某些参数的控制器调用某个功能?通过指令调用控制器功能参数

我会给变量myVar的范围。$ apply(attrs.whattodo);;

HTML:

<div ng-app="component"> 
    <div ng-controller="ctrl"> 
    <span ng-repeat="i in myarray"> 
    <span customattr whattodo="addVal">{{i}}</span> 
    </span> 
    </div> 

控制器JS:

function ctrl($scope) { 
     $scope.myarray = [1]; 
     $scope.addVal = function (value) { 
      $scope.myarray.push(value); 
     } 
    } 

指令JS:

angular.module('component', []).directive('customattr', function() { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attrs) { 
      var myVar = 5; 
      scope.$apply(attrs.whattodo); 
     } 
    }; 
}); 

回答

17

这里的工作方法之一:

你必须在这个范围内绑定属性与功能型示波器型号。所以,当你在其他(指令)需要你可以执行该SOPE

HTML

<body ng-controller="MainCtrl"> 
    Value: {{value}}! 

    <button customattr whattodo="addValue">Add</button> 
</body> 

JS

angular.module('component', []) 

.controller('MainCtrl', function($scope) { 
    $scope.value = 1; 

    $scope.addValue = function(val){ 
    alert(val); 
    $scope.value = val; 
    } 
}); 

.directive('customattr', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      whattodo: "=" // or ' someOtherScopeName: "=whattodo" ' 
     }, 
     link: function (scope, element, attrs) { 
      var myVar = 5; 
      scope.whattodo(myVar); // or ' scope.someOtherScopeName(myVar) ' 
     } 
    }; 
}); 

这里是code on plunker

fromAngularJS: Directives

= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localModel:'=myAttr' }, then widget scope property localModel will reflect the value of parentModel on the parent scope. Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel

+0

非常感谢。最后一件事:我给的例子是一个愚蠢的例子。但是用我的“真实”例子:这个值是我用一些输入文本呈现的一系列字典。如果我把空的范围或范围,所有我的输入字段变空。任何想法为什么?谢谢 – JohnCastle 2013-04-21 21:34:26

+0

老实说很难想象它,将你的真实世界变成笨蛋并不复杂吗?谢谢 – Maksym 2013-04-21 21:37:22

+0

当然!这里是:http://plnkr.co/edit/mu9DM1dEyZ36UqVvw0DM 只需在指令中将范围设置为true,您将在其中看到“Toto”。或 – JohnCastle 2013-04-22 06:36:43

10
在HTML

whattodo="addVal(value)" 

在指令

scope.$apply(function(s){ 
    s.whattodo({value : myVar}); 
}); 
+0

我:类型错误:对象#的特性 'whattodo' 不是一个功能? – JohnCastle 2013-04-21 21:09:45

+0

对不起。我纠正了它。应该是s.whattodo – Ketan 2013-04-21 21:41:19

+0

非常有趣,并且效果很好([demo](http://jsfiddle.net/BinaryMuse/rhXDs/))。经过一番思考,同样的事情必须用在像'

2

为什么不使用隔离范围中的“&”符号?

<body ng-controller="MainCtrl"> 
    Value: {{value}}! 
    <button customattr add-val="addValue(value)">Add</button> 
</body> 

在控制器:

function ctrl($scope) { 
     $scope.myarray = [1]; 
     $scope.addValue = function (value) { 
      $scope.myarray.push(value); 
     } 
    } 

而且在指令:

angular.module('component', []).directive('customattr', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      addVal: "&" 
     }, 
     controller: function ($scope) { 
      var myVar = 5; 
      // To execute addVal in controller with 'value' param 
      $scope.addVal({value: value}) 
     } 
    }; 
}); 
+1

在使用addVal的指令中,改用whattodo – userRaj 2015-11-19 10:53:18