Angular 2: ¿Es posible acceder a variables de referencia de plantilla desde la clase Component?
<div>
<input #ipt type="text"/>
</div>
¿Es posible acceder a la variable template access desde la clase component?
Es decir, puedo acceder aquí,
class XComponent{
somefunction(){
//Can I access #ipt here?
}
}
73
1 answers
Que es un caso de uso para @ViewChild
: https://angular.io/docs/ts/latest/api/core/index/ViewChild-decorator.html
class XComponent{
@ViewChild('ipt') input: ElementRef;
ngAfterViewInit(){
// this.input is NOW valid !!
}
somefunction(){
this.input.nativeElement......
}
}
Aquí hay una demostración que funciona: https://plnkr.co/edit/GKlymm5n6WaV1rARj4Xp?p=info
import {Component, NgModule, ViewChild, ElementRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<input #ipt value="viewChild works!!" />
</div>
`,
})
export class App {
@ViewChild('ipt') input: ElementRef;
name:string;
constructor() {
this.name = 'Angular2'
}
ngAfterViewInit() {
console.log(this.input.nativeElement.value);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
105
Author: mxii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-22 06:46:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-22 06:46:32