Respuestas:
Ese es un caso de uso para @ViewChild:
https://angular.io/docs/ts/latest/api/core/index/ViewChild-decorator.html
class XComponent {
@ViewChild('ipt', { static: true }) input: ElementRef;
ngAfterViewInit() {
// this.input is NOW valid !!
}
somefunction() {
this.input.nativeElement......
}
}
Aquí hay una demostración funcional:
https://stackblitz.com/edit/angular-viewchilddemo?file=src%2Fapp%2Fapp.component.ts
ngAfterViewInit()se disparó el evento . Tienes que importar ViewChilddesde '@ angular / core` ..
this.ipt.nativeElement.setAttribute('value', 'xxx');pero no pasa nada. Y no hay métodos como value()o setValue(), incluso si lo declaro de tipo HTMLInputElement (baso esto en el código de sugerencia / autocompletar del IDE). En mi caso, no me importa leer el valor. Solo necesito establecer diferentes valores.
setPropertytambién?
this.input.nativeElement.value = 'test'funcionar ?! tal vez haya comportamientos especiales con las formas y sus vinculaciones.