Angular:如何在指令控制器中启动作为指令属性给出的范围函数? [英] Angular: how to launch scope function given as directive attribute in directives controller?
问题描述
嘿,
我在其他控制器中有指令(有自己的控制器),这样:
Hey,
I have directive (with own controller) within other controler like this:
<div id="Test-content" ng-controller="testCtrl">
<oa-modal-dialog dialog-title="This is example dialog" dialog-state="true" dialog-on-open="" dialog-on-close="test.close()" dialog-type="warning" dialog-size="small" dialog-top-toolbar="" dialog-bottom-toolbar=""></oa-modal-dialog>
</div>
测试控制器:
Test controller:
.controller('testCtrl', ['$scope', function ($scope) {
$scope.test = {
close: function() {
console.log('test');
}
};
}])
模态对话框指令:
Modal dialog directive:
.directive('oaModalDialog', function () {
return {
restrict: 'E',
transclude: true,
link: function(scope, element, attrs) {
attrs.$observe('dialogState', function (value) {
scope.dialogState = value;
});
},
templateUrl: 'oaModalDialogs.html',
controller: 'modalDialogsCtrl'
}
});
问题如何检索和触发指令属性'对话框'中的函数-close'在指令控制器'modalDialogs'?假设任何函数都可以包含在属性中,所以我不能使用固定函数(控制器中的$ scope.test.close())
我试过像这个:
Question how can I retrieve and fire function which is in directives attribute 'dialog-on-close' in directives controller 'modalDialogs'? Presume that any function can be included into attribute so I can't use fixed function ($scope.test.close() in the controller)
I tried something like this:
.controller('modalDialogsCtrl', ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
$attrs['dialogOnClose']();
}]);
引发错误......任何建议?
Throws error... Any suggestions?
推荐答案
范围',功能(
scope', function (
范围){
scope.test = {
close:function(){
console.log(' test');
}
};
}])
scope.test = { close: function() { console.log('test'); } }; }])
模态对话框指令:
Modal dialog directive:
.directive('oaModalDialog', function () {
return {
restrict: 'E',
transclude: true,
link: function(scope, element, attrs) {
attrs.
这篇关于Angular:如何在指令控制器中启动作为指令属性给出的范围函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!