简体   繁体   English

模型的Ember动态输入值-HTMLbars

[英]Ember Dynamic Input Value from Model - HTMLbars

Assuming the following model: 假设以下模型:

export default Ember.Route.extend({
  model() {
    return ['car', 'truck', 'boat'];
  }
});

I can loop through the model like so: 我可以像这样遍历模型:

{{#each model as |vehicle index|}}
    {{vehicle}} - #{{index}}
{{/each}}

which would result in this: 这将导致:

car - #0
truck - #1
boat - #2

However, what if I wanted dynamic properties in an input (I know the following does not work the way I want): 但是,如果我想要输入中的动态属性该怎么办(我知道以下内容无法按我想要的方式工作):

{{#each model as |vehicle index|}}
    {{input value=vehicle}}
{{/each}}

Which results in all of the input fields loading with the value already defined: car, truck, and boat in the input. 这将导致所有输入字段都加载已定义的值:输入中的汽车,卡车和船。

What I want to make happen is this (assuming you looped through the model, it would 'output' this): 我想要实现的是这个(假设您遍历了模型,它将“输出”此信息):

{{input value=car}}
{{input value=truck}}
{{input value=boat}}

but it's not outputting that, it outputs this: 但是它没有输出,而是输出:

{{input value="car"}}
{{input value="truck"}}
{{input value="boat"}}

The values are already defined but I would like to define 'car' in the model (meaning it has no value) I'm only setting up the name of the input and then later, I would be able to access {{car}} so that whatever the user types in the blank input, it outputs to {{car}} 值已经定义,但是我想在模型中定义“ car” (意味着它没有值),我仅设置输入的名称,然后,我将能够访问{{car}}这样,无论用户在空白输入中键入什么内容,它都会输出到{{car}}

Looking over the Ember docs, they show an example of Binding dynamic attribute to an input helper 查看Ember文档,它们显示了将动态属性绑定到输入助手的示例。

To refer back to the above example the final code would be: 回到上面的示例,最终代码将是:

{{#each model as |vehicle index|}}
    {{input value=(mut (get this vehicle))}}
{{/each}}

And if you reference {{car}} in your document, as you type in the input... it would display. 并且,如果您在文档中引用{{car}} ,则在输入内容时会显示...。

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

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