私は 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}}
コントローラは次のとおりです。
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
toggleEditor: function () {
this.set('isEditing', !this.get('isEditing'));
},
save: function () {
var object = this.get('object');
var property = this.get('property');
object.set(property, this.get('value'));
var promise = object.save();
promise.finally(() => {
this.send('toggleEditor');
});
}
}
});
パラメータをtrueautofocus="autofocus"
に設定すると、使用が機能します。isEditing
ただし、アンカー要素が表示されているときにユーザーがリンクをクリックしても、フォーカスは新しく表示された入力要素に移動しません。したがって、私の質問は次のとおりです。入力要素に焦点を当てる最良の方法は何ですか? 内部toggleEditor
では、ID で入力要素にアクセスする方法と、Ember を使用してそれをフォーカスする方法を教えてください。