Ember.js 如何在Ember 2中集中特定的输入元素

Ember.js 如何在Ember 2中集中特定的输入元素,ember.js,ember.js-2,Ember.js,Ember.js 2,我正在学习Ember 2,并尝试编写一个简单的内联编辑器。我的问题是自动聚焦输入元素。组件的模板如下所示: {{#if isEditing}} {{input type="text" placeholder="Line item" autofocus="autofocus" value=value class="form-control" focus-out="save"}} {{/if}} {{#unless isEditing}} <a href="#" {{actio

我正在学习Ember 2,并尝试编写一个简单的内联编辑器。我的问题是自动聚焦输入元素。组件的模板如下所示:

{{#if isEditing}}
    {{input type="text" placeholder="Line item" autofocus="autofocus" value=value class="form-control" focus-out="save"}}
{{/if}}
{{#unless isEditing}}
    <a href="#" {{action "toggleEditor"}}>{{value}}</a>
{{/unless}}

使用
autofocus=“autofocus”
可在将
isEditing
参数设置为true时工作。但是,当锚元素可见,并且用户单击链接时,焦点不会转移到新可见的输入元素。因此,我的问题是:聚焦输入元素的最佳方式是什么?在
toggleEditor
中,如何通过ID访问输入元素,以及如何使用余烬对其进行聚焦

有一种更好的切换属性的方法

this.toggleProperty('propertyName');

也考虑使用I/OR.< /P>

{{#if isEditing}}
    {{input type="text" placeholder="Line item" class="my-input"}}
{{else}}
    <a href="#" {{action "toggleEditor"}}>{{value}}</a>
{{/if}}

奇怪的东西tho。

谢谢你,克丽丝特詹!成功了。还有一个很好的例子,就是切换属性
{{#if isEditing}}
    {{input type="text" placeholder="Line item" class="my-input"}}
{{else}}
    <a href="#" {{action "toggleEditor"}}>{{value}}</a>
{{/if}}
toggleIsEditing: function() {
        this.toggleProperty('isEditing');

        if(this.get('isEditing')) {
            Ember.run.scheduleOnce('afterRender', this, function() {
                $('.my-input').focus();
            });  
        }
},