filter: notarray error in angularjs

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

http://plnkr.co/edit/cJsScs8ixF1aq85Ri7nV?p=preview

filter is not working. Other part of code also breaks. Throwing error filter:notarray. how it can be fixed

<head>   <link rel="stylesheet" href="style.css">  </head>  <body ng-init="items=[3,1,2,3];">   <h1>Hello Plunker!</h1>    <div >    </div>   <input type="text" ng-model="nm" />      <div ng-repeat="item in items track by $index | filter:nm" ng-hide="hide">     {{item}}    </div>    <button ng-click="hide=!hide">Toggle </button>   <button ng-click="items[items.length]=items.length">Add</button>     <script src="https://code.angularjs.org/1.4.2/angular.min.js"></script>   <script src="script.js"></script> </body>  </html> 

回答1:

The documentation for ng-repeat says:

track by must always be the last expression

So you need to change this line:

<div ng-repeat="item in items track by $index | filter:nm" ng-hide="hide"> 

to this:

<div ng-repeat="item in items | filter: nm track by $index" ng-hide="hide"> 

I know it's obscure, and this one catches people out. Often the documentation isn't on the page you expect it to be (e.g. filter) but is still in a logical place (e.g. ng-repeat). It 'should' all be there.



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