gpt4 book ai didi

javascript - 来自指令 Controller 的 Angular 调用 Controller 函数

转载 作者:行者123 更新时间:2023-12-03 05:22:52 24 4
gpt4 key购买 nike

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

我有 Controller a:

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

$scope.MyParentFunction = function(){

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

};

}

Controller 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;

});

如何从指令 Controller 调用“MyParentFunction()”?提前致谢!

最佳答案

希望这会有所帮助:

您所需要的只是绑定(bind)您的父函数

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

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

查看 angularjs 文档

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

关于javascript - 来自指令 Controller 的 Angular 调用 Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41299246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com