简体   繁体   English

angularjs-ng-repeat仅适用于$ index

[英]angularjs - ng-repeat works with $index only

I am new to AngularJS and I have to say I am finding it exceedingly difficult and fiddly. 我是AngularJS的新手,我不得不说我发现它极其困难和轻率。

I have a json response from a webserver. 我有来自网络服务器的json响应。 Typical array of dictionaries. 典型的字典数组。 I want to display the name key for each dictionary in the values array. 我想在values数组中显示每个字典的name键。 This works: 这有效:

<ul>
  <li ng-model="projects" ng-repeat="values in projects">
    project name:{{projects.values[$index].name}}
  </li>
</ul>

This does NOT work, but I don't understand why it doesn't: 这行不通,但是我不明白为什么行不通:

<ul>
  <li ng-model="projects" ng-repeat="values in projects">
    project name:{{values.name}}
  </li>
</ul>

Plain old {{name}} doesn't work either. 普通的旧{{name}}也不起作用。 None of the examples I've found resort to calling out the array index explicitly with $index , so I must be doing something wrong. 我发现的所有示例都没有使用$index显式调用数组$index ,因此我必须做错了什么。 Please enlighten me! 请赐教!

you don't need ng-model on your list anywhere. 您不需要在任何地方使用ng-model。

here is a working example of what you're trying to do: 这是您要尝试执行的工作示例:

http://jsfiddle.net/8r2zA/1/ http://jsfiddle.net/8r2zA/1/

and as the others said, if you need access to the index in your iteration you can do: 正如其他人所说,如果您需要在迭代中访问索引,则可以执行以下操作:

<ul>
  <li ng-repeat="(index, values) in projects">
    {{projects[index].name}}
  </li>
</ul>

It would be good, to see yours projects structure. 看到您的projects结构会很好。
But, you could also try: 但是,您也可以尝试:

<li ng-repeat="(index, name) in projects">
  {{name[index]}}
</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM