代码之家  ›  专栏  ›  技术社区  ›  hackp0int

角5中的DOM操作

  •  1
  • hackp0int  · 技术社区  · 6 年前

    <div class="btn-wrapper-bt1"> <button>AAA</button> </div>

    node_modules/somebt

    ngOnInit

    2 回复  |  直到 6 年前
        1
  •  1
  •   David    6 年前

    #ref

    <div #ref >
       <your-3rd-party-component></your-3rd-party-component>
    </div>
    

    import { Component,Renderer2, ViewChild,ElementRef } from '@angular/core';
    
    export class YourParentComponent  {
    
    
      @ViewChild('ref') containerEltRef: ElementRef;
    
      constructor(private renderer: Renderer2)
      {
    
      }
    
      ngAfterViewInit()
      {
        // retrieves element by class 
        let elt = this.containerEltRef.nativeElement.querySelector('.btn-wrapper-bt1');
        this.renderer.addClass(elt, 'newClass'); //Adds new class to element
    
      }
    }
    

    stacklblitz demo

    :host ::ng-deep .btn-wrapper-bt1
    {
      color: red;
    }
    
        2
  •  0
  •   Jscti    6 年前

    <div #myRef class="btn-wrapper-bt1">
       <button>AAA</button>
    </div>

    @ViewChild('myRef') myElement: ElementRef;
    
    myFunc(){
        // do whatever you want with it AFTER you third party module finished its job (that's your call)
        //this.myElement.nativeElement.querySelector()
        //this.myElement.nativeElement.classList.remove('toto')
    }