【问题标题】:What is the {{#each as}} syntax in ember?ember 中的 {{#each as}} 语法是什么?
【发布时间】:2015-07-20 13:29:46
【问题描述】:

我在试图找出有关控制器的一些东西时遇到了这个问题How to set itemController in each as (ember 1.11 beta3)?,提问者在他们的{{#each}} 中使用了这个语法

{#each content as |product index|}}
  {{index}}
{{/each}}

我以前从未见过这个,也找不到任何关于它的文档。有人可以解释一下这是做什么的吗?

【问题讨论】:

    标签: ember.js handlebars.js


    【解决方案1】:

    这个语法是block params introduced in Ember 1.10。关注此链接将为您提供更多信息,但基本上它允许您对集合进行迭代,仍然具有内部可访问的控制器(或组件)范围。

    这里的索引基本上就是一个索引。

    {{somePropertyOnController}}
    {#each content as |product index|}}
      <!-- index is an index in iteration -->
      {{index}}
      <!-- product is an object in the array / enumeration -->
      {{product}}
      <!-- still have access to controller -->
      {{somePropertyOnController}}
    {{/each}}
    

    【讨论】:

    • 那么如果您不需要在each 块内使用控制器属性,它是否等同于{{#each thing in content}} 还是应该始终使用1.10 后的管道格式?
    • 它是等价的,但是你问的语法被贬低了。新版本为您提供相同的功能和上下文。
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2014-05-27
    • 1970-01-01
    • 2020-06-01
    • 2013-10-17
    • 2020-04-18
    相关资源
    最近更新 更多