Angularjs 动态改变指令函数
根据以下指令:Angularjs 动态改变指令函数,angularjs,Angularjs,根据以下指令: app.directive("myDirective", function() { return { restrict: "AE", scope: { myFunction: '&' }, templateUrl: "some_template.html",
app.directive("myDirective", function() {
return {
restrict: "AE",
scope: {
myFunction: '&'
},
templateUrl: "some_template.html",
link: function (scope, element) {
}
}
});
我宣布它为
<my-directive my-function="function1()"></my-directive>
但我的问题是,在控制器中,我需要根据条件调用
function1
或function2
。是否有办法动态更改my function
属性中的函数?是的,只需将其设置为引用一个函数或另一个函数的某个变量即可:
<my-directive my-function="functionToCall()"></my-directive>
检查一个工作示例
<button type="button" ng-click="functionToCall = function1">Switch to function 1</button>
<button type="button" ng-click="functionToCall = function2">Switch to function 2</button>