How to listen for value changes from class property TypeScript - Angular

前端 未结 5 1204
情话喂你
情话喂你 2020-12-13 06:57

In AngularJS, we can listen variable change using $watch, $digest... which is no longer possible with the new versions of Angular (5, 6).

I

5条回答
  •  -上瘾入骨i
    2020-12-13 07:24

    I think I came into the way to listen to DOM changes that you can get any changes that do to your element, I really hope these hints and tips will help you to fix your problem, following the following simple step:

    First, you need to reference your element like this:

    in HTML:

    ....

    And in your TS file of that component:

    @ViewChild('someElement')
    public someElement: ElementRef;
    

    Second, you need to create an observer to listen to that element changes, you need to make your component ts file to implements AfterViewInit, OnDestroy, then implement that ngAfterViewInit() there (OnDestroy has a job later):

    private changes: MutationObserver;
    ngAfterViewInit(): void {
      console.debug(this.someElement.nativeElement);
    
      // This is just to demo 
      setInterval(() => {
        // Note: Renderer2 service you to inject with constructor, but this is just for demo so it is not really part of the answer
        this.renderer.setAttribute(this.someElement.nativeElement, 'my_custom', 'secondNow_' + (new Date().getSeconds()));
      }, 5000);
    
      // Here is the Mutation Observer for that element works
      this.changes = new MutationObserver((mutations: MutationRecord[]) => {
          mutations.forEach((mutation: MutationRecord) => {
            console.debug('Mutation record fired', mutation);
            console.debug(`Attribute '${mutation.attributeName}' changed to value `, mutation.target.attributes[mutation.attributeName].value);
          });
        }
      );
    
      // Here we start observer to work with that element
      this.changes.observe(this.someElement.nativeElement, {
        attributes: true,
        childList: true,
        characterData: true
      });
    }
    

    You will see the console will work with any changes on that element:

    This is another example here that you will see 2 mutation records fired and for the class that changed:

    // This is just to demo
    setTimeout(() => {
       // Note: Renderer2 service you to inject with constructor, but this is just for demo so it is not really part of the answer
      this.renderer.addClass(this.someElement.nativeElement, 'newClass' + (new Date().getSeconds()));
      this.renderer.addClass(this.someElement.nativeElement, 'newClass' + (new Date().getSeconds() + 1));
    }, 5000);
    
    // Here is the Mutation Observer for that element works
    this.changes = new MutationObserver((mutations: MutationRecord[]) => {
        mutations.forEach((mutation: MutationRecord) => {
          console.debug('Mutation record fired', mutation);
          if (mutation.attributeName == 'class') {
            console.debug(`Class changed, current class list`, mutation.target.classList);
          }
        });
      }
    );
    

    Console log:

    And just housekeeping stuff, OnDestroy:

    ngOnDestroy(): void {
      this.changes.disconnect();
    }
    

    Finally, you can look into this Reference: Listening to DOM Changes Using MutationObserver in Angular

提交回复
热议问题