我有一个简单的场景
<thead grid-columns pager-info="pagerInfo" onsort="onSort()">
<tr>
<th width="15%"><column sortby='FName'>Name</column></th>
<th width="17%"><column sortby='Mobile'>Number</column></th>
</tr>
</thead>
的想法是,因为我所有的列都需要定义一个onsort
,我就可以把它定义一次父。
但是,为什么我的孩子指令(列)没有将它的$parent
设置为grid-columns指令?相反,scope.$parent
设置为控制器作用域,所以我无法解决如何从我的子指令访问我的父指令作用域。
这两个指令有一个孤立的范围,我的孩子指令transcludes:
ngApp.directive('gridColumns', function() {
// Attribute-based wrapper round the columns that allows us to capture the pagerInfo and set a sort handler.
return {
restrict: 'A',
scope: {
pagerInfo: '=',
onsort: '='
}
};
});
ngApp.directive('column', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
sortby: '@'
},
template: "<span><a ng-click='sort()' ... </span>",
link: function (scope, el, attrs) {
scope.sort = function() {
// I want to call scope.$parent.onsort, but scope.$parent is the controller !
// scope.$parent.onsort(scope.sortby);
};
}
};
});
可以通过这个网址@ http://stackoverflow.com/questions/20212354/angularjs-accessing-parent-directive-properties-from-child -directives –