1
我是新來的角度和使用我的自定義指令ng-repeat有問題。我想顯示一些我從休息界面得到的帖子,然後在指令中使用它們的_id屬性用於其他目的。但是,事實證明,當從函數內部使用(下面的示例中的測試)時,該屬性始終是最後顯示的帖子中的一個。當試圖直接在viewmodel上顯示id時,它顯示正確的。希望這是有道理的。任何幫助,將不勝感激。ng-repeat覆蓋屬性的功能,但不在視圖模型
//directive.js
angular.module('app').directive('gnPost', myGnPost);
function myGnPost() {
return {
restrict: 'E',
controller: 'postCtrl',
controllerAs: 'postCtrl',
bindToController: {
post: '='
},
scope: {},
templateUrl: 'template.html'
};
};
//controller.js
angular.module('app').controller('postCtrl', myPostCtrl);
function myPostCtrl(postRestService) {
vm = this;
vm.test = function() {
return vm.post._id;
};
};
// template.html
<p>{{postCtrl.post._id}}</p>
//displays the right id
<p>{{postCtrl.test()}}</p>
//displays the id of the last element of ng-repeat
//parent page.html
<gn-post ng-repeat="singlePost in posts.postList" post="singlePost"></gn-post>
謝謝,這有助於。沒有注意到缺失的變種。 – Rakem