从组件类访问模板参考变量 [英] Access template reference variables from component class

查看:58
本文介绍了从组件类访问模板参考变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div>
   <input #ipt type="text"/>
</div>

是否可以从组件类访问模板访问变量?

Is it possible to access the template access variable from the component class?

即,我可以在这里访问它吗,

i.e., can I access it here,

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

这篇关于从组件类访问模板参考变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆