指令指令通信和呼叫通过功能
问题描述:
我有两个指令和一个控制器,它有一个功能测试。我的日期指令有一个按钮,点击正在调用过滤器指令中的函数,这需要调用通过输入的函数,我试着做$scope.$apply($attrs.entering);
,但我得到$apply already in progress
。指令指令通信和呼叫通过功能
在HTML我有:
<filter entering="ctrl.test()">
<date></date>
</filter>
app.directive('filter', function() {
return {
restrict: 'E',
controller: function($scope, $element, $attrs) {
this.sayHello = function() {
// call function here $scope.$apply($attrs.entering);
}
}
}
}
app.firective('date', function() {
return {
require: '^filter',
link: function(scope, element, attrs, filterCtrl) {
scope.isClicked = function() {
filterCtrl.say()
}
}
}
}
答
不能使用$范围角控制器,因为它们在$范围已经结束了内$ $申请隐含申请。您可以使用$ scope。$ watch或$ broadcast/$ emit/$,具体取决于场景。
附加信息:http://jimhoskins.com/2012/12/17/angularjs-and-apply.html