angular2 * ngfor和嵌套的可观察对象

时间:2016-07-26 15:16:13

标签: angular angular2-template

嵌套observable有问题:

我设法得到了正确数量的计划, 顺便说一句,所有可靠的设置都很好,我可以在控制台中看到状态的数量正在增长...
但是组合框是空的我做错了什么?

以下是我在模板中的代码片段:

<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>

我的组件中的另一个:

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.
}

我得到的结果是我的计划表,但在同一行上组合是空的:组合中没有选择(异步isn&#39; t工作?) 似乎状态$更新时模板没有更新

感谢帮忙!

2 个答案:

答案 0 :(得分:0)

我认为这就是你想要的:

        <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>

答案 1 :(得分:0)

我通过修改模板和组件来解决我的问题:

<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>

和组件:

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.
}