【问题标题】:In Ember, can I yield input tag to component and not break my form?在 Ember 中,我可以为组件生成输入标签而不破坏我的表单吗?
【发布时间】:2015-11-24 14:16:05
【问题描述】:

我在想也许我在 EmberJS 的 yield 函数中发现了一个错误。

我想要实现的是,表单的一个组件,如果需要的话,我是否会产生额外的输入标签。我的例子比较复杂,但我在Ember Twiddle 中重现了“bug”。

基本上,我有组件:

form-component/template.hbs

<form {{action 'componentAction' on='submit'}}>
  {{yield}}
  <button type="submit">Submit</button>
</form>

我想在那里产生输入标签

应用程序/模板.hbs

{{#form-component
  action="controllerAction"}}
  {{input value=name placeholder='Name'}}
{{/form-component}}

如您所见,它在 Twiddle 中不起作用。 (它应该将表单下方的文本更新为您的输入)

但如果我将输入 {{input value=name placeholder='Name'}}application/template.hbs 移动到组件,它可以工作:

更新的表单组件/template.hbs

<form {{action 'componentAction' on='submit'}}>
  {{yield}}
  {{input value=name placeholder='Name'}}
  <button type="submit">Submit</button>
</form>

更新的应用程序/template.hbs

{{#form-component
  action="controllerAction"}}
{{/form-component}}

这是一个错误,还是我在这里遗漏了一些明显的东西?

【问题讨论】:

  • 这不是错误。您输入的值将在您声明它的组件或控制器中设置。如果您在 application/template.hbs 中创建输入,则该值将在模板控制器中设置。如果在组件中创建,值会在form-component中设置

标签: javascript ember.js ember-cli


【解决方案1】:

如果要在表单组件中设置值,可以这样:

//form-component/template.hbs
{{#form-component
  action="controllerAction" as |myForm|}}
  {{input value=myForm.name placeholder='Name'}}
{{/form-component}}


//application/template.hbs
<form {{action 'componentAction' on='submit'}}>
    {{yield this}}
  <button type="submit">Submit</button>
</form>

【讨论】:

  • 您知道是否可以在使用this method 获得多个收益的同时做到这一点?我的意思是{{yield body this}}
猜你喜欢
  • 2011-03-31
  • 2021-03-31
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 2016-10-22
  • 2014-01-27
  • 1970-01-01
相关资源
最近更新 更多