微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular 2/typescript:获取模板中的元素

有人知道如何获取在组件模板中定义的元素吗? polymer使它很容易与$和$$。
我只是想知道如何去在Angular 2。

以教程中的示例为例:

/**
 * Created by amgu on 9/14/2015.
 */
import {Component} from '@angular/core'

@Component({
    selector:'display'
    template:`

     <input #myname (input) = "updateName(myname.value)"/>
     <p> My name : {{myName}}</p>

    `

})
 export class displayComponent{
    myName: string;
    updateName: Function;
    constructor(){

        this.myName = "Aman";
        this.updateName = function(input: String){

            this.myName = input;

        };

    }

}

如何从类定义中捕获’P’或’input’元素的引用?

你可以通过注入到组件的构造函数中,通过ElementRef获取DOM元素的句柄:
constructor(myElement: ElementRef) { ... }

文件https://angular.io/docs/ts/latest/api/core/index/ElementRef-class.html

原文地址:https://www.jb51.cc/angularjs/147145.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐