简体   繁体   English

如何动态更改 Angular 10 中的 ngModel 值

[英]How can I dynamically change ngModel value in Angular 10

In Angular 10, I'm dynamically generating three dropdowns and need to dynamically generate their ngModel values (eg, row1, row2, row3).在 Angular 10 中,我正在动态生成三个下拉列表,并且需要动态生成它们的 ngModel 值(例如,row1、row2、row3)。

<table>
     <tr *ngFor="let num of [0, 1, 2]; let i=index">
       <td>
         <h4>Row {{ num + 1 }}</h4>
         <select matNativeControl [(ngModel)]="row[num+1]">
             <option *ngFor="let item of items" [value]="item">{{ item.fruit }}</option>
           </select>
       </td>
     </tr>
   </table>

(ngModel)]="row[num+1]" is where I'm trying to add a 1, 2, 3 as the dropdowns multiply. (ngModel)]="row[num+1]"是我尝试在下拉菜单中添加 1、2、3 的地方。 The header is dynamically changing but ngModel is not and is throwing an error as it is finding row to be undefined. header 是动态变化的,但 ngModel 不是,并且在发现row未定义时抛出错误。 I've tried several different ways and have hit a wall.我尝试了几种不同的方法并且碰壁了。

Here is code from app.component.ts:这是来自 app.component.ts 的代码:

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  row: any;
  row1: any;
  row2: any;
  row3: any;
  items: any;
  num: any;

  constructor() {}

  ngOnInit(): void {
    this.items = [
      { fruit: 'apple', color: 'red' },
      { fruit: 'orange', color: 'orange' },
      { fruit: 'grape', color: 'purple' }
    ];
  }

  // printVal() {
  //   console.log('Test: ', this.row[this.num].fruit);
  // }
}

Here is stackblitz: https://stackblitz.com/edit/angular-ivy-9wr3vh这是stackblitz: https://stackblitz.com/edit/angular-ivy-9wr3vh

row[num] isn't equal to row1. row[num] 不等于 row1。 It is searching a variable called row,then it's index.它正在搜索一个名为 row 的变量,然后是索引。 since you're row is undefined, it is impossible to attach ngModel to it.由于您的行未定义,因此无法将 ngModel 附加到它。 try something like this,尝试这样的事情,

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  row = [0,1,2]
  items: any;

  constructor() {}

  ngOnInit(): void {
    this.items = [
      { fruit: 'apple', color: 'red' },
      { fruit: 'orange', color: 'orange' },
      { fruit: 'grape', color: 'purple' }
    ];
  }

  // printVal() {
  //   console.log('Test: ', this.row[this.num].fruit);
  // }
}

And if you're trying to change items, color or fruit -i don't know which one you're trying to change.- try this in your html如果你想改变物品、颜色或水果——我不知道你想改变哪一种。——在你的 html 中试试这个

<table>
     <tr *ngFor="let item of items; let i=index">
       <td>
         <h4>Row {{ i + 1 }}</h4>
         <select matNativeControl [(ngModel)]="item[i].color">
             <option *ngFor="let item of items" [value]="item">{{ item.fruit }}</option>
           </select>
       </td>
     </tr>
   </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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