arrays - NgFor 在使用数组创建下拉选择时仅支持绑定到数组等可迭代对象
问题描述
运行应用程序时出现以下错误:
错误错误:找不到类型为“subLandscape”的不同支持对象“[object Object]”。NgFor 仅支持绑定到 Iterables,例如 Arrays。
我在 HTML 中引用的 subLandscape 变量是一个数组,但在将它与“ngFor”一起使用时会引发错误
HTML:
<div class="form-group" [class.has-error]="subLandscape.invalid && subLandscape.touched">
<strong>Sub Landscape</strong>
<br>
<p>Please choose from one of the Sub Landscape options. Sub Landscape options are for reporting only.</p>
<label class="control-label">SubLandscape:</label>
<mat-select #subLandscape="ngModel" type="text" class="form-control" name="subLandscape" [ngModel]="model.subLandscape">
<mat-option *ngFor="let item of subLandscape">
{{ item }}
</mat-option>
</mat-select>
<div *ngIf="subLandscape.invalid && subLandscape.touched" class="alert alert-danger">
Select the Sub Landscape from the provided list.
</div>
</div>
模型:
export class SModel {
constructor (
public description: string,
public reasons: string,
public projectName: string,
public subLandscape: string,
public centerdata: string,
public nom: number,
public size: string,
public dbgh: string
) {
}
}
零件:
import { Component, OnInit } from '@angular/core';
import { Standalone } from '../standalone';
import { StandaloneModel } from '../models/standalone.model';
import {MatTableModule, MatTableDataSource} from '@angular/material/table';
@Component({
selector: 'app-standalone-form',
templateUrl: './standalone-form.component.html',
styleUrls: ['./standalone-form.component.css']
})
export class StandaloneFormComponent {
public project: string[] = ['', 'appdev', 'apptest', 'appqa'];
public subLandscape: string[] = ['DEV', 'Testing', 'QA', 'UAT'];
public dataCenter: string[] = ['PDC', 'SDC'];
public model = new SModel('', '', '', '', '', 0, '', '');
}
解决方案
可能是因为它无法区分subLandscape
from
#subLandscape="ngModel"
和组件属性数组public subLandscape: string[] = ['DEV', 'Testing', 'QA', 'UAT'];
。改个名字应该可以解决问题
推荐阅读
- file - 写入大约 2 GB Vertx 的文件时出现内存不足错误
- javascript - 通过数组元素的Javascript滑块
- python - 创建vectors.dense和sparse.dense,它们是一样的吗?
- php - LaratrustSeeder 类不存在
- botframework - MS Teams submitTask 无法在 IOS Teams 应用程序中提交给机器人
- html - 我的图像(在 div 下标记)无法并排坐在同一行
- ios - 如何添加像模态视图(iOS)这样的联系人应用程序?
- javascript - 使用 React 功能组件时如何防止重新渲染
- blockchain - 区块链上存在比特币地址
- filesystemwatcher - C# fileSystemWatcher,将文件名添加到列表框问题