でFormControlを作成するためにフォーカスを設定しない私は、動的にFormGroupを追加で入力フィールドにフォーカスを設定することはできていないようですまたはAbstractControl?はどのように私はプログラムで動的にAngular2
答えて
DOMエレメントではないため、FormControl
またはAbstractControl
に設定することはできません。あなたがしなければならないことは、どういうわけか彼らに要素の参照があり、その上に.focus()
と呼ぶことです。これはViewChildren
(現在のところ、APIドキュメントは存在しない、2016-12-16)を通じて実現できます。あなたのコンポーネントクラスで
:
import { ElementRef, ViewChildren } from '@angular/core';
// ...imports and such
class MyComponent {
// other variables
@ViewChildren('formRow') rows: ElementRef;
// ...other code
addNewRow() {
// other stuff for adding a row
this.rows.first().nativeElement.focus();
}
}
あなたが最後の子に注力したい場合は...のようなthis.rows.last().nativeElement.focus()
そして、あなたのテンプレートで何か:
<div #formRow *ngFor="let row in rows">
<!-- form row stuff -->
</div>
EDIT:
私は実際にあなたが探しているものをやっている誰かのCodePenを見つけましたhttps://codepen.io/souldreamer/pen/QydMNG
これは安全な方法では、次のように上記の回答のすべてを組み合わせ、角度5に対する角度
@Component({
selector: 'my-comp',
template: `
<input #myInput type="text" />
<div> Some other content </div>
`
})
export class MyComp implements AfterViewInit {
@ViewChild('myInput') input: ElementRef;
constructor(private renderer: Renderer) {}
ngAfterViewInit() {
this.renderer.invokeElementMethod(this.input.nativeElement,
'focus');
}
}
真。ただし、Rendererは廃止されました。既存のRenderer2には 'invokeElementMethod'メソッドがありません。 – user776686
によってお勧め:
コンポーネント関連コード:
import { AfterViewInit, QueryList, ViewChildren, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
// .. other imports
export class MyComp implements AfterViewInit, OnDestroy {
@ViewChildren('input') rows: QueryList<any>;
private sub1:Subscription = new Subscription();
//other variables ..
// changes to rows only happen after this lifecycle event so you need
// to subscribe to the changes made in rows.
// This subscription is to avoid memory leaks
ngAfterViewInit() {
this.sub1 = this.rows.changes.subscribe(resp => {
if (this.rows.length > 1){
this.rows.last.nativeElement.focus();
}
});
}
//memory leak avoidance
ngOnDestroy(){
this.sub1.unsubscribe();
}
//add a new input to the page
addInput() {
const formArray = this.form.get('inputs') as FormArray;
formArray.push(
new FormGroup(
{input: new FormControl(null, [Validators.required])}
));
return true;
}
// need for dynamic adds of elements to re
//focus may not be needed by others
trackByFn(index:any, item:any){
return index;
}
テンプレートロジックは次のようになります。
<div formArrayName="inputs" class="col-md-6 col-12"
*ngFor="let inputCtrl of form.get('phones').controls;
let i=index; trackBy:trackByFn">
<div [formGroupName]="i">
<input #input type="text" class="phone"
(blur)="addRecord()"
formControlName="input" />
</div>
</div>
私のテンプレートでは、ぼかしのレコードを追加しますが、次の入力フィールドを動的に追加するボタンを簡単に設定することもできます。重要な部分は、このコードでは、新しい要素が必要に応じてフォーカスを取得することです。
私はあなたが
- 1. アンドロイド:どのようにプログラム的に私のAndroidの活動で
- 2. は、どのように私はPythonでモジュールのプログラム的
- 3. はどのように私はプログラム的にHTMLとJavaScript
- 4. Angular2展開は - 自動的に私のAngular2アプリケーションではindex.htmlを
- 5. はどのようにプログラム的
- 6. どのように私は私のangular2フォームで
- 7. はどのようにangular2
- 8. どのように私はVimは自動的に
- 9. は、どのように私は動的に
- 10. は、私はプログラム的に次のように構築したいプログラムで
- 11. どのように動的なコンテンツにAngular2パイプを適用するには?
- 12. は、私は、次のシャイニーアプリを持ってどのようにプログラム的にテンプレート
- 13. はネイティブリアクト:どのように動的に
- 14. はどのように自動的に
- 15. はどのように私はasp.net動的コンテンツ
- 16. どのように私は、動的に[OK]をウェブサイト
- 17. は、どのように私はPythonプログラム私はオランダに住んで
- 18. アンドロイド:どのようにAndroidのではプログラム的
- 19. 私のJavaプログラムはどのように動くのですか? (ArrayList)
- 20. は、どのように私はより具体的には
- 21. Angular2どのように私は別のコンポーネント
- 22. 私のJavaプログラムでLiveGraphを動的に使用するには
- 23. どのように私は、自動リブートサーバ
- 24. はどのようにangular2ディレクティブ
- 25. どのように自動的にアンドロイドでのGPSのオンとオフをプログラムで
- 26. は、どのように私はこのプログラムの開発
- 27. はどのようにプログラムでJUnitテスト
- 28. Angular2、どのように私は親から子
- 29. どのように私はこのようなangular2でJのSONオブジェクトを作成したいangular2
- 30. はのUITableViewプログラムで、私は次のようにプログラムで私のテーブルビューを作成
お聞かせ私はそれが '@ViewChildren( 'formRow')行だと思う:QueryList;' –