【问题标题】:accessing dynamic input values in ember #each block in ember访问 ember 中的动态输入值 #each ember 中的块
【发布时间】:2016-07-24 19:51:44
【问题描述】:

我在访问组件中的输入值时遇到问题。我正在尝试在我的模板中动态创建值绑定并使用 this.controller.get("pst"+id) 在 conponent.js 文件中进行访问,但是结果不足。使用 Ember 2.2

{{#each post in |pst idx|}}
    {{input value=(concat 'pst' idx)}}
{{/each}}

【问题讨论】:

  • Ember 版本?试试{{#each post as |pst idx|}}
  • 创建绑定值的变量似乎是个问题。 concat 辅助函数构建一个字符串,但是当我在动态创建的字符串上尝试 this.get.controller("..") 时,它不会返回输入框的值(并返回未定义)
  • 我相信您需要的是 get 助手,可能与 concat 结合使用:(get this (concat "pst" idx))
  • 我没有骰子就试过了。我也试过 this.get('idx'+i);

标签: javascript ember.js handlebars.js htmlbars


【解决方案1】:

好吧,it works as expected,但你为什么要这样做?

请说明您要存档的内容,然后我们可以提供更好的帮助。

需要明确的是,使用 get 助手生成的值是不可变的。

为什么不做类似{{input value=pst}} 的事情呢? 如果这不是一个选项,您可能应该在 JS 中构建您的数组,然后在把手中使用它!

【讨论】:

    【解决方案2】:

    定义一个计算属性,将您的“post”变量包装在您的 component.js 文件中。迭代该包装器。我认为这是一种生成动态值的强大方法。

    您的模板:

    {{#each postWrappers as postWrapper}}
        {{input value=postWrapper.value}}
    {{/each}}
    

    你的 component.js:

    postWrappers : Ember.computed('post', function() { 
       //your concat code
    });
    

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 2015-04-10
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      相关资源
      最近更新 更多