简体   繁体   中英

angular array observable subscription not triggering

I have an array of type Person my state.

export interface Person {
    name: string;
    age: number;
}

I have a component that contains an Observable of this state slice.

export class PassengersComponent implements OnInit {
    public passengers$: Observable<Person[]>;

    constructor(public store: Store<fromRoot.State>) { }
    ngOnInit() {
            this.passengers$ = this.store.select(fromStore.getPassengers);

            // subscribe to the observable to detect any changes
            this.passengers$.subscribe((data: Person[]) => {
                console.log('Subscription hit!'); //<--- NOT BEING HIT AFTER 1ST TIME
            });

    }
}

I push out the object array to the view simply.

<input type="number" *ngFor="let p of (passengers$ | async)" [ngModel]="p.age" (ngModelChange)="onPassengerAgeChange($event, p.name)">

The "onPassengerAgeChange()" event fires off, and my store gets updated successfully. I can verify this by adding:

{{ (passengers$ | async) | json }}

However for some reason, the subscription code in my component does not trigger when the store gets updated. What am I missing here?

I was incorrectly changing (mutating) the state of my Person[] array.

I had

{
    return state = action.payload;
}

where it should have been

{
    return state = [].concat(action.payload);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM