AngularJS Ng-repeat is not working as expected

假如想象 提交于 2019-12-13 07:35:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!