[英]Angular share data from directive controller to controller
我想知道是否有任何方法可以在沒有事件的情況下從指令控制器共享數據。 可以說我有:
.directive('someHandler', function () {
return {
controller: function () {
this.users = [];
}
}
})
這是對他的孩子指令通信負責的父母指令。 我想每次都說這個數組的變化讓主控制器知道。 像“ =”的功能;
這是我的想法,希望它能起作用。 嘗試使用指令的控制器
.directive('someHandler', function () {
return {
//...previous arguments
controller: function ($scope, $element, $attrs) {
$scope.YourFunction = function () {
//Your code
}
}
});
然后在您的控制器中調用$ scope.YourFunction() 。
加成
如果要使用父指令中的函數,則必須在子指令中包括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();
}
});
閱讀這篇文章 ,您將知道何時使用鏈接和控制器。 有關更多詳細信息,請閱讀這篇精彩的文章: https : //thinkster.io/egghead/directive-communication/
最后,我希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.