How can I select an element in a component template?

后端 未结 12 2543
挽巷
挽巷 2020-11-21 06:56

Does anybody know how to get hold of an element defined in a component template? Polymer makes it really easy with the $ and $$.

I was just

12条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 07:24

    import the ViewChild decorator from @angular/core, like so:

    HTML Code:

    ... ...

    TS Code:

    import { ViewChild } from '@angular/core';
    
    class TemplateFormComponent {
    
      @ViewChild('f') myForm: any;
        .
        .
        .
    }
    

    now you can use 'myForm' object to access any element within it in the class.

    Source

提交回复
热议问题