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

前端 未结 7 1009
我在风中等你
我在风中等你 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:49

    I don't like accessing the dom in Angular but this use case you may need to. The only way to disable the annoying auto complete seems to be to add the attribute "readonly" and remove it after the form loads.

    ngAfterViewInit() {
          window.setTimeout(function () {
    
             var arr: HTMLCollection = document.getElementsByClassName('form-control');
             for (var i = 0; i < arr.length; i++) {
               if (arr[i].hasAttribute("readonly")) {
                 arr[i].removeAttribute('readonly');
               }
             }
    
       }, 500);
    }
    
    0 讨论(0)
提交回复
热议问题