我有這個網站結合在AngularJS
<div ng-app="myApp">
<parent>
<child></child>
</parent>
</div>
transclude和指導繼承和下面的角碼
var myApp = angular.module('myApp',[]);
myApp.directive('parent', function() {
return {
transclude: true,
link: function(scope, element, attrs) {
scope.getContent = function() {
console.log('called getContent');
}
},
restrict: 'E',
template: '<span>parent <span ng-transclude></span></span>'
}
});
myApp.directive('child', function() {
return {
restrict: 'E',
scope:true,
template: '<div>child</div>',
link: function(scope, element, attrs) {
scope.getContent();
}
}
});
的jsfiddle以上是here
我在這個例子中的問題是我可以看到繼承工作和跨越孤立工作,但當我嘗試將兩者結合起來時,子指令不知道父範圍,他函數getContent。所以沒有console.log,並且在那之前,scope.getContent未定義的子指令錯誤。
我意識到,這可能是孩子的指令不再是一個孩子被橫掃,所以我想我需要開始玩編譯方法中的post和prelink函數?還是我需要在父級定義一個自定義transclude函數?
的代碼或進一步閱讀任何指針讚賞 - 我已閱讀並在這裏對這種事情類似的問題,但我發現很難遵循,希望有人能解決我的Hello World的kickstart我的理解
在此先感謝
好的,謝謝。我沒有意識到,你可以使用這樣的前後鏈接方法,我認爲你必須在編譯方法 – alfonsob 2014-10-16 19:20:33
中定義它們,這標誌着這是一個正確的答案,因爲它有助於理解在這種情況下事件發生的順序,這是我的主要懷疑爲什麼這不是爲我工作 - 謝謝floribon – alfonsob 2014-10-16 19:29:38