我創建了一個簡單的可重複使用的收集容器作爲隔離範圍的指令,transclude
選項設置爲true。如何將一個函數傳遞給一個控制器(angular 1.3)的transcluded scope?
app.directive('itemWrapper', function() {
return {
template: '...',
replace: true,
transclude: true,
restrict: 'E',
scope: {
name: '=',
isExpanded: '='
}
};
});
和列表視圖的指令,在這裏我想從將要處理的列表項點擊控制器傳遞函數
app.directive('listItem', function() {
return {
template: '...',
replace: true,
restrict: 'E',
scope: {
item: '=',
action: '&'
},
link: function(scope, elm, attrs){
scope.performAction = function(val){
action({'data': val});
};
}
};
});
我的HTML的大塊看起來在以下方式:
<collection-wrapper name='item.name' is-expanded='item.visible'>
<list-item item='item' action='log(data)'></list-item>
</collection-wrapper>
但是,當我點擊鏈接,我得到一個參考錯誤,說action
沒有定義。問題是,如何從控制器傳遞一個函數到這個指令?據我瞭解,跨越範圍是一個孤立範圍的孩子,這是我無法克服的障礙!
這是Plunkr。
該死的......我一直忽略指令中的'範圍'沒有理由。謝謝。 – ChernikovP