How to trigger a change event manually - angular2

前端 未结 2 1238
故里飘歌
故里飘歌 2020-12-15 18:10

Given the following components:

@Component({
    selector: \'compA\',
    template:  template: ``
})
export class Comp         


        
相关标签:
2条回答
  • 2020-12-15 18:55

    As far as I understood that unless the complete item object is changed, angular2 does not recognize the changes on item.

    It's not all that straightforward. You have to distinguish between triggering ngOnChanges when object is mutated and DOM update of the child component. Angular doesn't recognize that item is changed and doesn't trigger a ngOnChanges lifecycle hook, but the DOM will still be updated if you reference particular property of the item in the template. It's because the reference to the object is preserved. Therefore to have this behavior:

    And afterwards, make the child component i.e. CompB re-rendered as if angular detected a change in the regular way.

    You don't have to do anything in particular because you will still have update in the DOM.

    Manual change detection

    You can insert a change detector and trigger it like this:

    @Component({
        selector: 'compA',
        template:  template: `<compB [item]=item></compB>`
    })
    export class CompA {
        item:any;
        constructor(cd: ChangeDetectorRef) {}
    
        updateItem():void {
            item[name] = "updated name";
            this.cd.detectChanges();
        }
    }
    

    This triggers change detection for the current component and all its children.

    But, it won't have any effect in your case because even though Angular doesn't detect change in item it still runs change detection for the child B component and updates the DOM.

    Unless you use ChangeDetectionStrategy.OnPush. In this case a way to go for you would be to do a manual check in the ngDoCheck hook of the CompB:

    import { ChangeDetectorRef } from '@angular/core';
    
    export class CompB implements OnInit{
        @Input() item: any;
        someArray: any[];
        previous;
    
        constructor(cd: ChangeDetectorRef) {}
    
        ngOnInit():void {
            this.previous = this.item.name;
            someArray.push("something");
        }
    
        ngDoCheck() {
          if (this.previous !== this.item.name) {
            this.cd.detectChanges();
          }
        }
    }
    

    You can find more information in the following articles:

    • Everything you need to know about change detection in Angular article.
    • The mechanics of DOM updates in Angular
    • The mechanics of property bindings update in Angular
    0 讨论(0)
  • 2020-12-15 18:58

    you can put another input in CompB so when you want change properties of item in CompA just change value of this input.

    @Component({
        selector: 'compA',
        template:  template: `<compB [item]=item [trigger]=trigger></compB>`
    })
    export class CompA {
        item:any;
        trigger: any;
        updateItem():void {
            item[name] = "updated name";
            trigger = new Object();
        }
    }
    
    @Component({
        selector: 'compB',
        template:  template: `<p>{{item[name]}}</p>`
    })
    export class CompB implements OnInit{
        @Input() item: any;
        @Input() trigger: any;
    }

    0 讨论(0)
提交回复
热议问题