角2 /打字稿:在模板得到的元件的保持状态 [英] angular 2 / typescript : get hold of an element in the template

查看:121
本文介绍了角2 /打字稿:在模板得到的元件的保持状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何机构知道如何从withing类组件模板中定义的元素的定义搁置?聚合物使得它真正用$和$$容易。
只是不知道如何去它的角2。

any body know how to get hold of an element defined in a component template from withing the class defined? Polymer makes it really easy with the $ and $$. Just wondering how to go about it in Angular 2.

从教程就拿例如:

/**
 * Created by amgu on 9/14/2015.
 */
import {Component, View, bootstrap} from 'angular2/angular2'

@Component({

    selector:'display'

})
@View({

    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'或'输入'元素的引用保持在类定义中?

How do I catch hold of a reference of the 'P' or 'input' element from within the class definition?

推荐答案

您可以通过注入到你的组件的构造获得通过 ElementRef 的句柄DOM元素:

You can get a handle to the DOM element via ElementRef by injecting it into your component's constructor:

constructor(myElement: ElementRef) { ... }

文件:<一href=\"https://angular.io/docs/js/latest/api/core/ElementRef-class.html\">https://angular.io/docs/js/latest/api/core/ElementRef-class.html

这篇关于角2 /打字稿:在模板得到的元件的保持状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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