angular - 在 Angular 中绑定数据后,Select 根本不显示
问题描述
我正在尝试将国家数据绑定到注册表单中的选择选项中,但绑定后选择完全消失了。这是代码:
数据模型
export class Countries {
public name: string;
public code: string;
constructor(name: string, code: string) {
this.name = name;
this.code = code;
}
}
export const country: Countries [] = [
{
"name": "United States",
"code": "US"
},
{
"name": "Netherlands",
"code": "NL"
},
{
"name": "United Kingdom",
"code": "UK"
},
]
我剪切了整个数据,因为这里显示太长了。但它看起来像这样。
打字稿
import {country} from "../../models/countries.model"
...
export class SignupComponent implements OnInit {
country[];
SignupForm: FormGroup;
...
ngOnInit() {
this.nav.hide();
this.SignupForm = new FormGroup({
'username': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])'),
]),
'password': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}'),
]),
'country': new FormControl([Validators.required,
]),
});
}
我认为错误应该在 TypeScript 中。这里也是 HTML 部分:
HTML
<div class="form-group form-signup">
<input class="form-control form-control-lg" placeholder="Password" type="password" formControlName="password"/>
<span *ngIf="!SignupForm.get('password').valid && SignupForm.get('password').touched" class="help-block">Please enter a valid password!</span>
</div>
<div class="form-group form-signup">
<select class="form-control form-control-lg" *ngFor="let countries of country" [value]="countries.name" formControlName="country">
<option value="">--Please choose an option--</option>
<option> {{ countries.name }}
</option>
</select>
<span *ngIf="!SignupForm.get('country').valid && SignupForm.get('country').touched" class="help-block">Please enter a country!</span>
</div>
怎么了?为什么 Select 完全消失并且绑定不起作用?如何解决?
解决方案
你必须重复option
标签而不是select
标签:
<select class="form-control form-control-lg">
<option value="">--Please choose an option--</option>
<option *ngFor="let countries of country" [value]="countries.name" formControlName="country"> {{ countries.name }} </option>
</select>
问候,
推荐阅读
- scala - 使用 Spark UDF 提取 tar.gz 时出现 OutOfMemoryError
- reactjs - 如何用 axios 编写请求体?
- google-apps-script - .clearcontent 不能只处理一个电子表格
- sql-injection - 为什么查询中的雪花变量绑定会抛出表名错误而不是整数?
- pytorch - 如何加载多任务模型但只预测其中一项任务?
- google-sheets - 将 n 张纸的第一列合并为一个列
- python - 循环通过雅虎财经导入并保存每只股票的名称
- android - 如何在 Neumorphism 设计概念中更改 Android 中的按钮背景?
- c - 过度使用 gcc 消毒剂
- java - 任务“:openCVLibrary310:compileDebugAidl”执行失败