我有一個新的角2應(yīng)用程序和一個列表input盒子。當(dāng)用戶按下返回鍵時,我將添加一個新的input框位于當(dāng)前正在編輯的框后面。或者說,我(異步地)在模型中的數(shù)組中添加了一個新條目,這將導(dǎo)致角2自動生成一個新的input在不久的將來。我怎樣才能做到input自動將焦點更改到新添加的元素?編輯1:或者,我得到一個模型對象的引用,該對象導(dǎo)致生成DOM。從組件代碼中,是否有一種方法可以搜索表示特定模型對象的DOM元素?編輯2:這是我的代碼,讓這一切順利進行。希望這是足以冒犯一些角度2的發(fā)展,以鼓勵回答:-)app.WordComponent = ng.core
.Component({
selector: 'word-editor',
template:'<input type="text" [value]="word.word" (input)="word.update($event.target.value)" (keydown)="keydown($event)"/>',
styles:[
''
],
properties:[
'list:list',
'word:word'
]
})
.Class({
constructor:[
function() {
}
],
keydown:function(e) {
if(e.which == 13) {
var ul = e.target.parentNode.parentNode.parentNode;
var childCount = ul.childNodes.length;
this.list.addWord("").then(function(word) {
var interval = setInterval(function() {
if(childCount < ul.childNodes.length) {
ul.lastChild.querySelector('input').focus();
clearInterval(interval);
}
}, 1);
});
}
}
});
3 回答

BIG陽
TA貢獻1859條經(jīng)驗 獲得超6個贊
這可能就是你要找的:
import?{Component,?ViewChild}?from?'angular2/core' @Component({ ??selector:?'my-app', ??providers:?[], ??template:?` ????<div> ??????<input?#name> ????</div> ??`, ??directives:?[] }) export?class?App?{ ??@ViewChild('name')?vc:?ElementRef; ??ngAfterViewInit()?{ ????this.vc.nativeElement.focus(); ??} }
- 3 回答
- 0 關(guān)注
- 548 瀏覽
添加回答
舉報
0/150
提交
取消