Ember.js为标签上的属性设置

Ember.js为标签上的属性设置,ember.js,Ember.js,我有一个HTMLBars模板,它有一个标签和一个余烬输入组件,我想知道在标签引用组件上设置for属性的最佳方法是什么。是否有办法从组件外部访问组件id。我知道您可以手动设置组件上的id,但我希望避免这样做,以免导致重复id冲突我个人将输入上的id手动设置为输入字段和当前组件id串联的结果 因此,在我的my form.hbs中,我这样做: <label for=(concat elementId '-username')>Username</label> {{input i

我有一个HTMLBars模板,它有一个标签和一个余烬输入组件,我想知道在标签引用组件上设置for属性的最佳方法是什么。是否有办法从组件外部访问组件id。我知道您可以手动设置组件上的id,但我希望避免这样做,以免导致重复id冲突

我个人将
输入上的id手动设置为输入字段和当前组件id串联的结果

因此,在我的
my form.hbs
中,我这样做:

<label for=(concat elementId '-username')>Username</label>
{{input id=(concat elementId '-username') value=username}}

<label for=(concat elementId '-password')>Username</label>
{{input id=(concat elementId '-password') value=username}}
使用label/template.hbs输入


你知道这个解决方案有什么副作用吗?有没有办法从route上的一个模板获得一个唯一的id,而不是把所有的东西都放在组件中?@jpoiri你可以在初始化器中重新打开
Ember.Controller
,并在其中添加一个
uniqId
属性,你可以调用
Ember.guidFor(this)
@我认为没有。
export default Ember.Component.extend({
  tagName: '',
  inputId: Ember.computed({
    get() {
      return Ember.guidFor(this)+'-inpudid';
    }
  })
});
<label for=inputId>{{label}}</label>
{{input id=inputId value=value placeholder=label}}
{{input-with-label value=username label="Username"}}
{{input-with-label value=password label="Password"}}