Angular2 * ngfor and nested observables

There was a problem with nested observables:

I have the correct plan number, btw all wariable are well configured, I see in the console that the number of statuses is growing ...
but the combo box is empty, what am I doing wrong?

Here is the code snippet that I have in my template:

<tr *ngFor="let plan of plans$ | async">
    <td><input type="text" [(ngModel)]="plan.libelle"/></td>
    <td>
        <select>
            <option *ngFor="let statut of statuts$ | async"
                    ngValue="{{statut.id}}"
                    [selected]="statut.id == plan.statut.id">
                {{statut.libelle}}
            </option>
        </select>
    </td>
</tr>

different in my component:

private plans$:Observable<Array<Plan>>;
private statuts$:Observable<Array<Param>>;

constructor(private planService:PlanService, private paramService:ParamService) {}

ngOnInit() {
    this.statuts$ = this.paramService.typesPlan$; //return the observable from the service
    this.plans$ = this.planService.plans$; //same thing
    this.paramService.loadAll('plan'); //this load all the status in an async way.
    this.planService.loadAll(); //the same as above and it work.
}

As a result, I have a table with my plans, but in the same line the combo is empty: there is no choice in the combo (does the asynchronous mode not work?) It looks like the template does not update when the status $ is updated

Thanks for the help!

+4
source share
2 answers

I will solve the problem by changing the temple and the component:

<tr *ngFor="let plan of plans$ | async">
<td><input type="text" [(ngModel)]="plan.libelle"/></td>
<td>
    <select>
        <option *ngFor="let statut of statuts"
                ngValue="{{statut.id}}"
                [selected]="statut.id == plan.statut.id">
            {{statut.libelle}}
        </option>
    </select>
</td>

and component:

private plans$:Observable<Array<Plan>>;
private statuts:Array<Param>;

constructor(private planService:PlanService, private paramService:ParamService) {}

ngOnInit() {
    this.paramService.typesPlan$.suscribe((status)=> this.status = status); 
    this.plans$ = this.planService.plans$; 
    this.paramService.loadAll('plan'); //this load all the status in an async way.
    this.planService.loadAll(); //the same as above and it work.
}
0
source

, , :

        <option *ngFor="let statut of statuts$ | async"
                [(ngValue)]="statut.id">
            {{statut.libelle}}
        </option>

        <option *ngFor="let statut of statuts$ | async"
                [ngValue]="plan.statut.id"
                (ngValueChange)="status.id = $event">
            {{statut.libelle}}
        </option>
0

Source: https://habr.com/ru/post/1649178/


All Articles