指令控制器中的角度调用控制器功能

时间:2016-12-23 10:04:05

标签: javascript angularjs directive

我有一个问题应该很简单,但我找不到答案。

我有控制器a:

app.controller('myController', function ($scope){

    $scope.MyParentFunction = function(){ 

              alert("it does not work..");

    };     

}

和控制器b,它在一个指令中:

app.directive('myDirective', function () {

var directive = {};

    //properties
    directive.scope = {
        date: '=ngModel',
    },

    //controller
    directive.controller = function ($scope) {

        $scope.CallParentFunction = function(){

          //Call Function From Parent scope here
          $scope.MyParentFunction();     

        }

    },

    //call parent function in template
    directive.template = '<div ng-click="CallParentFunction()">Call</div>'; 

    return directive;

});

我如何调用&#34; MyParentFunction()&#34;从我的指令控制器? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

您只需绑定父函数

即可
directive.scope = {
    date: '=ngModel',
    callParentFunction: "&"  //function binding 
}

http://plnkr.co/edit/y7I1g7bKok8NOADipqX4?p=preview

看一下angularjs文档

https://docs.angularjs.org/guide/directive