How to get/set/remove element attribute in Angular 2 using “the angular way”?

前端 未结 7 1010
我在风中等你
我在风中等你 2021-01-17 15:48

I\'ve been reading some articles about Angular 2 pitfalls and what to avoid, one of those things revolves around not accessing the DOM directly.

I noticed that the

7条回答
  •  借酒劲吻你
    2021-01-17 16:35

    In case someone is still looking for this (as I did), i shall add up a bit on David's answer which was on Angular's native renderer.

    You have all this requested functionality in newest Angular Renderer2

    Particularly if you want to completely remove attributes (ex. invalid aria tags in community components that fail accessibility tests) from elements and not set their value to null, there is

    renderer2.removeAttribute(elementRef.nativeElement, 'AttributeName');
    

    EDIT: You should use AfterViewInit() lifecycle, as described in other answers, as the initial view must be rendered before you make any custom DOM changes.

提交回复
热议问题