我正在嘗試使用某些分層數據來構建應該是一個非常簡單的自定義指令。列表中的每個頁面都有子頁面,數據格式如下:ng-repeat內部自定義角度指令不工作
{"Title 1": "Page Title", "Id": "1", "Pages": {"Title 1.1": "Page Title 1.1", "Id": "2"}, {"Title 1.2": "Page Title 1.2", "Id": "3"}}
等等。這裏的數據只是一個簡單的例子 - 數據或檢索方法沒有問題。
我有一個指令控制器設置爲:
app.directive('pageSelectList', function() {
return {
restrict: 'EA',
replace: true,
scope: {
pageList: '='
},
templateUrl: '/Scripts/App/Directives/Templates/PageSelectList.html',
link: function (scope, elem, attrs) { }
};
});
模板是:
<ul class="page-list page-root">
<li ng-repeat="p in pageList.Pages">{{ p.Title }}</li>
</ul>
該指令與從父範圍($scope.listOfPages
)繪製數據使用。
但是,使用它時,什麼都不顯示 - 它是空白的。奇怪的是,與下面的標記替換指令模板時:
<p>{{ pageList.Title }}</p>
預期標記<p>Title 1</p>
所示。
看起來該指令與ng-repeat
有某種問題,或者重複中使用的數據是pageList
對象的子對象。
此外,當指令的標記僅用於具有來自父範圍的數據的常規頁面時,根本沒有問題。這似乎是指令本身的問題。
編輯
這是填充數據的指令頁面控制器:
var pageEditController = function ($scope, $rootScope, $state, $stateParams, pageService) {
$scope.page = {};
$scope.errorMessage = "";
getPage(); // This is for other data on the page and not directly linked to directive issue.
getPages(); // This is to get the directive data
function getPage() { // Just an http get method for $scope.page }}
function getPages() {
pageService.getTree()
.success(function (result) {
$scope.pageList = result.Result; // This is where the directive data is loaded into scope
})
.error(function (error) {
$scope.errorMessage = 'Unable to load pages';
});
};
});
而且奇怪的現象:
這個模板:
<p>{{ pageList.Title }}</p>
顯示標題OK。
此:
<p>{{ pageList.Title }}</p><p>{{ pageList.Id }}</p>
顯示什麼都沒有。原始示例中的ng-repeat顯然不起作用。
你能提供你實際使用的代碼嗎? – Nhor
你如何調用'pageSelectList'指令? – SaiGiridhar
你在控制檯中看到任何錯誤嗎?除了在OP中無效的JSON - 你的代碼似乎工作 – Grundy