我想知道是否有任何方法可以在没有事件的情况下将指令控制器中的数据共享到控制器。 让我说我有:
.directive('someHandler', function () {
return {
controller: function () {
this.users = [];
}
}
})
这是父指令,负责他的孩子指令沟通。 我想每次都让这个数组改变让我们知道主控制器。 就像“=”;
的功能一样答案 0 :(得分:0)
这是我的想法,希望它能奏效。尝试使用指令
的控制器.directive('someHandler', function () {
return {
//...previous arguments
controller: function ($scope, $element, $attrs) {
$scope.YourFunction = function () {
//Your code
}
}
});
然后在您的控制器中调用 $ scope.YourFunction()。
<强> ADDITION 强>
如果要使用父指令中的函数,则必须在子指令中包含 require 参数。通过这种方式,您可以建立层次关系并启用它们之间的通信。
之后,必须使用链接功能从父控制器调用该功能。
在您的情况下,子指令将如下所示:
.directive('childsomeHandler', function () {
return {
//...previous arguments
require:"^someHandler", //here is inheritance to make communication between child and parent diretctive
link: function (scope, element, attrs, someHandlerCtrl) {
someHandlerCtrl.YourFunction();
}
});
阅读此post,您将知道何时使用链接和控制器。 有关详细信息,请阅读这篇精彩的文章: https://thinkster.io/egghead/directive-communication/
最后,我希望这对你有所帮助。