Ember迭代:何时使用#each用户,#each控制器中的用户,#each模型中的用户等 [英] Ember iterations: when to use #each User, #each user in controller, #each user in model, etc

查看:108
本文介绍了Ember迭代:何时使用#each用户,#each控制器中的用户,#each模型中的用户等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑我应该如何迭代对象数组,何时使用不同的可能性。

I'm confused on how I'm supposed to iterate through arrays of objects and when to use the different possibilities.

到目前为止,我遇到了:

So far I've come across:

{{#each user in controller}}
{{#each user in model}}
{{#each user in users}}
{{#each User}}

我甚至把他们中的一些改了一些,看看我能不能打破它;例如

And I've even switched some of them up a bit just to see if I could break it; for example both

{{#each user in controller}}

{{#each user in model}}

成功输出相同的代码。我希望有人知道什么时候使用每个人的简单解释,以及它们之间的区别。谢谢!

output the same code successfully. I was hoping someone knew a simple explanation on when to use each one and what the differences between them are. Thanks!

推荐答案

以下使用 {{#each}} 帮助器是等效的:

The following uses of the {{#each}} helper are equivalent:

{{#each user in controller}}
{{#each controller}}
{{#each user in model}}
{{#each model}}
{{#each user in content}}
{{#each content}}
// and a couple of other possible combination, depending on your setup

由于这个 PR ,你也可以简单地使用 {{#each}}

And since this PR you can also use simply {{#each}}.

主要区别在于如何访问您正在循环的属性和项目本身。

The main difference though is how you access the properties and the items itself you are looping over.

使用 user 作为访问者的示例:

Example using user as the accessor:

{{#each user in model}}
  {{user.name}}
{{/each}}

使用这个的示例:

{{#each model}}
  {{this.name}}
{{/each}}

直接访问项目属性的示例:

Example accessing the item properties directly:

 {{#each model}}
   {{name}}
 {{/each}}

希望这能使事情更清楚。

Hope this makes things more clear.

这篇关于Ember迭代:何时使用#each用户,#each控制器中的用户,#each模型中的用户等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆