我一直在努力與此相當一段時間,我無法弄清楚如何解決這個問題。嵌套元素指令無法訪問父指令範圍
我想做一個網格指令,其中包含列指令來描述網格,但列不會是元素,只會將列添加到在網格指令的範圍內聲明的數組。
我想解釋這個問題的最好辦法是查看查看代碼:
var myApp = angular.module('myApp', [])
.controller('myCtrl', function ($scope, $http) {
})
.directive('mygrid', function() {
return {
restrict: "E",
scope: true,
compile: function ($scope) {
debugger;
$scope.Data = {};
$scope.Data.currentPage = 1;
$scope.Data.rowsPerPage = 10;
$scope.Data.startPage = 1;
$scope.Data.endPage = 5;
$scope.Data.totalRecords = 0;
$scope.Data.tableData = {};
$scope.Data.columns = [];
},
replace: true,
templateUrl: 'mygrid.html',
transclude: true
};
})
.directive('column', function() {
return {
restrict: "E",
scope: true,
controller: function ($scope) {
debugger;
$scope.Data.columns.push({
name: attrs.name
});
}
};
});
這裏是HTML標記:
<body ng-app="myApp">
<div ng-controller="myCtrl">
<input type="text" ng-model="filterGrid" />
<mygrid>
<column name="id">ID</column>
<column name="name">Name</column>
<column name="type">Type</column>
<column name="created">Created</column>
<column name="updated">Updated</column>
</mygrid>
</div>
此外,你可以測試jsfiddle中的實際代碼:http://jsfiddle.net/BarrCode/aNU5h/
我試着用compile,c控制器和鏈接,但由於某些原因,父網格的列未定義。
我該如何解決這個問題?
編輯: 當我刪除替換,templateUrl,transclude從mygrid指令,我可以從列指令的範圍。
感謝
這個成功嗎? –
是的,在更高版本的angularJS中,您可以使用$ scope。$ child childHead –