<div>
<input #ipt type="text"/>
</div>
是否可以从组件类访问模板访问变量?
也就是说,我可以在这里访问它吗,
class XComponent{
somefunction(){
//Can I access #ipt here?
}
}
最佳答案
这是 @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......
}
}
这是一个工作演示:
https://stackblitz.com/edit/angular-viewchilddemo?file=src%2Fapp%2Fapp.component.ts
关于angular - 从组件类访问模板引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39631594/