问题
I'm having a weird issue, I can access all the elements coming from my scope as data model, also I can see the scope elements in the Chrome console, but when I try using the model within a ng-repeat statement it does not work, it just does not loop through the vm.menu.items. Below is my simple code:
<body ng-controller="mainController">
<div ng-controller="app.views.layout.header as vm">
<p>{{vm.currentMenuName}} {{vm.menu.items[0].displayName}} {{vm.menu.items[1].displayName}}</p> **<<----- IT works!**
<ul>
<li ng-repeat="menuItem in vm.menu.items">
<span>Not working! </span>{{menuItem.displayName}}
</li>
</ul>
</div>
</body>
I'm still pretty new to angular, so any help that point me into the right direction will be greatly appreciated.
回答1:
I removed the 'vm.' in your html and everything works as expected
<body ng-controller="mainController">
<div ng-controller="app.views.layout.header as vm">
<p>{{currentMenuName}} {{menu.items[0].displayName}} {{menu.items[1].displayName}}</p>
<ul>
<li ng-repeat="menuItem in menu.items">
<span>Not working! </span>{{menuItem.displayName}}
</li>
</ul>
</div>
http://plnkr.co/edit/1BtC8p1ViqKXSbCWE3Kj?p=preview
Or you can us 'this' in your controller instead of $scope (see http://plnkr.co/edit/1BtC8p1ViqKXSbCWE3Kj?p=preview
来源:https://stackoverflow.com/questions/29236166/angularjs-ng-repeat-is-not-working-as-expected