zoukankan      html  css  js  c++  java
  • [Angular] Using the platform agnostic Renderer & ElementRef

    ElementRef:

    ElementRef is a way to access native html element, notice that it only works for Broswer.

    Render:

    Render helps to take care of different platforms (mobile or browser). It is recommended to use Render to change DOM instead of using ElementRef directly.

            <label>
              Email address
              <input type="email" name="email" ngModel #email>
            </label>
    export class AuthFormComponent implements AfterViewInit {
    
      @ViewChild('email') email: ElementRef;
    
      constructor(
        private renderer: Renderer
      ) {}
    
      ngAfterViewInit() {
        this.renderer.setElementAttribute(this.email.nativeElement, 'placeholder', 'Enter your email address');
        this.renderer.setElementClass(this.email.nativeElement, 'email', true);
        this.renderer.invokeElementMethod(this.email.nativeElement, 'focus');
        // this.email.nativeElement.setAttribute('placeholder', 'Enter your email address');
        // this.email.nativeElement.classList.add('email');
        // this.email.nativeElement.focus();
      }
    
    ...
    
    }

    Another example:

    this.loadingService.duration: '2s'

    //from   
      this.el.nativeElement.style.setProperty(
          "--duration",
          this.loadingService.duration
        );
    
    
    // to
        this.render.setAttribute(
          this.el.nativeElement,
          "style",
          `--duration:${this.loadingService.duration}`
        );
  • 相关阅读:
    软解析和硬解析
    oracle存储过程常用技巧
    jquery-1.11.1.js
    JavaScript遍历table
    JavaScript向select下拉框中添加和删除元素
    glog
    DDL引发的对象invalidation
    模拟cursor pin S wait on X
    rsync 排除目录
    JavaScript解决select下拉框中的内容太长显示不全的问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6436461.html
Copyright © 2011-2022 走看看