Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 动态改变指令函数_Angularjs - Fatal编程技术网

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>