Angular 用JSON中的值填充Select
我对Angular 用JSON中的值填充Select,angular,angular-reactive-forms,Angular,Angular Reactive Forms,我对[formGroup](使用Angular 8)有一个问题,我有一个组件可以检查onInit,如果用户已经有项目或没有项目,并根据响应呈现值,如果有值,我就使用[selected]选项来呈现它们,这实际上有效,但只有视觉部分会导致当我尝试发布所选值时,我的验证器告诉我没有,下面是一个示例: 如果有人对这件事有所了解,我会很高兴听到的 Pricelist¤cy DIV(project.component.html) 接口.model.ts get f() { retu
[formGroup]
(使用Angular 8)有一个问题,我有一个组件可以检查onInit
,如果用户已经有项目或没有项目,并根据响应呈现值,如果有值,我就使用[selected]
选项来呈现它们,这实际上有效,但只有视觉部分会导致当我尝试发布所选值时,我的验证器告诉我没有,下面是一个示例:
如果有人对这件事有所了解,我会很高兴听到的
Pricelist¤cy DIV(project.component.html)
接口.model.ts
get f() {
return this.registerForm.controls;
}
requiredFields(){
this.registerForm = this.formBuilder.group(
{
title: ['',[Validators.required]],
country: ['',[Validators.required]],
pricelist: ['',[Validators.required]],
currency: ['',[Validators.required]],
}
)
}
project: {
id: number;
code: string;
title: string;
country_id: number;
network_height_id: number;
country_label: string;
created_at: any;
createdby_id: number;
createdby_name: string;
currency_code: string;
currency_id: number;
pricelist_id: number;
pricelist_label: any;
total_fourniture: number;
updated_at: any;
updatedby_id: number;
updatedby_name: string;
pricelist: Array<{
pricelist_id: number;
label: string;
}>
};
项目:{
id:编号;
代码:字符串;
标题:字符串;
国家编号:编号;
网络高度标识:编号;
国家/地区标签:字符串;
创建时间:任意;
createdby_id:number;
createdby_name:string;
货币代码:字符串;
货币标识:编号;
价目表编号:编号;
价格表标签:任何;
总数:个数;
更新地址:任何;
更新者_id:number;
更新人名称:字符串;
价格表:阵列
};
如果您还需要什么,请随时告诉我,谢谢您的时间然后我使用[所选]选项:不要这样做。Angular负责根据您的模型选择适当的选项。如果表单控件的值等于其中一个选项的值,则将选择该选项。如果模型是真理的话。请注意,value=“{p.id===project?.pricelist\u id}}}”
没有意义:您的所有选项都将以字符串“true”或字符串“false”作为其值。@jbnize谢谢,这真的很有帮助,因此如果我理解正确,[selected]
是一个“不能做”的选项,我应该只保留值
,我说得对吗?是的。与输入一样,您具有双向绑定:如果在输入中键入某个内容,则for控制值将更改,如果更改表单控制值,则输入显示的内容将更改。选择也是一样的。好的,谢谢,我会的
project: {
id: number;
code: string;
title: string;
country_id: number;
network_height_id: number;
country_label: string;
created_at: any;
createdby_id: number;
createdby_name: string;
currency_code: string;
currency_id: number;
pricelist_id: number;
pricelist_label: any;
total_fourniture: number;
updated_at: any;
updatedby_id: number;
updatedby_name: string;
pricelist: Array<{
pricelist_id: number;
label: string;
}>
};