我有一個指令,我希望其他指令能夠調用。我一直試圖使用指令控制器來嘗試實現這一點。指令所需的控制器無法找到
指令人會坐在同一頁上指示兩個,指令一個會叫的指令二的控制器暴露方法:
指令1:
'use strict';
angular.module('angularTestApp')
.directive('fileLibrary', function() {
return {
templateUrl: 'views/manage/file_library/file-library.html',
require: 'videoClipDetails',
restrict: 'AE',
link: function postLink(scope, element, attrs, videClipDetailsCtrl) {
scope.doSomethingInVideoClipDirective = function() {
videClipDetailsCtrl.doSomething();
}
}
};
});
指令二:
'use strict';
angular.module('angularTestApp')
.directive('videoClipDetails', function() {
return {
templateUrl: 'views/video_clip/video-clip-details.html',
restrict: 'AE',
controller: function($scope, $element) {
this.doSomething = function() {
console.log('I did something');
}
},
link: function postLink(scope, element, attrs) {
console.log('videoClipDetails directive');
//start the element out as hidden
}
};
});
文件,其中兩個被使用和設置爲兄弟姐妹:
<div>
<div video-clip-details></div>
<!-- main component for the file library -->
<div file-library></div>
</div>
我知道,閱讀文檔,我拿起了控制器可以當指令是相同的元素,這讓我覺得我可能會尋找這個問題的錯誤的方式上進行共享。任何人都可以讓我走上正軌嗎?
你說,分享控制器他們需要在同一個指令。您可能更容易使用$ broadcast和$ emit。 –