模板中的动态值 [英] dynamic value in template
问题描述
我已经扩展了一个视图(使用fieldId生成器函数)并设置了以下模板:
我需要为标签设置相同的计算IDfor属性(用view.fieldId计算)和{{view Em.Textfield ...}}
如何在{ {view Em.TextField expression ..}}?
任何其他替代方案?
template:Ember.Handlebars.compile('< label class =control-label{{bindAttr for =view.fieldId}}> {{view.label}}< / label> \\
< div class =controls> \
{{查看Em.TextField valueBinding =view.valueclass =input-large}} \
< ; span class =help-inline>可能出错了< / span> \
< / div>')
解决方案是绑定 for
Emax.TextField $ c的
elementId
属性的< label> $ c>。
首先,你必须通过设置它的 viewName
属性给一个名字给你的Ember.TextField :
{{view Ember.TextField viewName =myTextField}}
/ pre>
然后,您必须将其
elementId
绑定到属性,使用
bindAttr
:< label { {bindAttr for =view.myTextField.elementId}}>我的标签< / label>
它的工作原理只是尝试在这个JSFiddle 。
注意:另一个解决方案是嵌套
输入
内的标签。有关详细信息,请查看此SO问题:对标签使用Ember.js文本字段标识I'have extended a View (with a fieldId generator function) and set the following template:
I need to have the same computed id for the label "for attribute" ( computed with view.fieldId) and the {{view Em.Textfield ...}}
How to set the id attribute with view.fieldId in the {{view Em.TextField expression..}} ?
Any other alternative ?
template: Ember.Handlebars.compile('<label class="control-label" {{bindAttr for="view.fieldId"}}>{{view.label}}</label>\ <div class="controls">\ {{view Em.TextField valueBinding="view.value" class="input-large"}}\ <span class="help-inline">Something may have gone wrong</span>\ </div>')
解决方案The solution is to bind the
for
attribute of the<label>
to theelementId
property of theEmber.TextField
.First, you have to give a name to your Ember.TextField by settings its
viewName
property:{{view Ember.TextField viewName="myTextField"}}
Then, you have to bind its
elementId
to thefor
attribute, usingbindAttr
:<label {{bindAttr for="view.myTextField.elementId"}}> My label </label>
And it works, just try it in this JSFiddle.
N.B.: There is one other solution is to nest the
input
inside the label. For more information, take a look at this SO question: Using Ember.js text field ids for a tag这篇关于模板中的动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!