Angular使用 ElementRef + Renderer2 全局设置组件属性

1.引入元素选择器

 

 

 

2.使用angular钩子全局设置input长度

ngAfterViewChecked() {
    const input = this.el.nativeElement.querySelectorAll('input');
    if (input) {
      input.forEach(e => {
        this.renderer.setAttribute(e, 'maxlength', '1');
        // console.log(e);
      });
    }
  }

3.如果项目是采用framework式布局 可以全局设置容器内的页面组件的属性

 

 

 

posted @ 2019-11-22 17:18  官萧何  阅读(1363)  评论(0编辑  收藏  举报