【问题标题】:angular2 *ngfor and nested observableangular2 *ngfor 和嵌套的 observable
【发布时间】:2016-11-30 07:57:45
【问题描述】:

遇到了嵌套 observable 的问题:

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

这是我模板中的代码的 sn-p:

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

我得到的结果是一张包含我的计划的表格,但在同一行,组合是空的:组合中没有选择(异步不起作用?) 好像更新 status$ 时模板没有更新

感谢您的帮助!

【问题讨论】:

    标签: angular angular2-template


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我想这就是你想要的:

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

      【讨论】:

      • 当我有两个异步嵌套时会发生问题......如果我将选项标签放在 tr 之外,它就可以正常工作。但这不是我想要的:'(
      猜你喜欢
      • 2016-05-16
      • 2017-10-31
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2017-02-22
      • 2017-08-13
      • 1970-01-01
      • 2017-07-01
      相关资源
      最近更新 更多