小编Dav*_*vid的帖子

在Angular中将数组项绑定到[(值)]

嗨,我正在使用Angular材料,我有一个产品数组,在我的模板中创建一个表

 <tbody>
    <tr *ngFor="let item of productArray | async; let i = index">
Run Code Online (Sandbox Code Playgroud)

在这个循环中,我在<th>标签上有另一个循环:

<th>
   <mat-form-field>
      <mat-select placeholder="Length" formControlName="lengthOption" [compareWith]="compareWith" [(value)]="item.lengthOption">
         <mat-option *ngFor="let lengthOption of item.lengthOptions" [value]="lengthOption">
              {{lengthOption.name}}
         </mat-option>
      </mat-select>
   </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

我想利用双向绑定[(value)].

我知道我可以设置[value]为lengthOption.name然后设置绑定[(Value)]="selected",然后我可以在我的组件中设置(selected = whatever)它或在模板中查看它{{selected}}.

我的查询是否可以从父数组中获取此值,就像我在内循环中尝试一样:

*ngFor="let item of productArray | async; let i = index"

[(value)]="item.lengthOption"
Run Code Online (Sandbox Code Playgroud)

productOrray上存在lengthOption.

这一点我想为每个产品mat-select设置一个初始选择值.

lengthOption看起来像 { "id": 1, "name": "Ten Years" }

所以我试图将对象设置为父数组的mat-option,而不仅仅是一个对象值形成自己的数组.

提前致谢.

material-ui angular

8
推荐指数
1
解决办法
895
查看次数

标签 统计

angular ×1

material-ui ×1