【问题标题】:How do i initialize a formcontrol with an initial value, Validators.Required and disabled: true using FormBuilder?如何使用 FormBuilder 初始化具有初始值 Validators.Required 和 disabled: true 的表单控件?
【发布时间】:2019-03-06 01:08:05
【问题描述】:

https://angular.io/api/forms/FormControl 我检查了 Angular 网站上的 FormControl 页面,看看它是否有一个构造函数,允许我设置初始值、禁用模式和验证器,但它似乎没有这样的构造函数。所以我的问题是,是否可以初始化一个具有值、已禁用并设置了 Validators.required 的 formcontrolname?

这是我迄今为止尝试过的:

this.temperatureSettingForm = fb.group({
  'country' : [{value: '', disabled: true},Validators.required],
  'cities' : {value: '', disabled: true},
  'checkBox' : {value: false, disabled: true},
  'months': fb.group({
  'january' : {value: '', disabled: true},
  'february' : {value: '', disabled: true},
  'march' : {value: '', disabled: true},
  'april' : {value: '', disabled: true},
  'may' : {value: '', disabled: true},
  'june' : {value: '', disabled: true},
  'july' : {value: '', disabled: true},
  'august' : {value: '', disabled: true},
  'september' : {value: '', disabled: true},
  'october' : {value: '', disabled: true},
  'november' : {value: '', disabled: true},
  'december' : {value: '', disabled: true}
  })
});

【问题讨论】:

标签: angular angular-reactive-forms


【解决方案1】:

是的,它确实有这样的constructor

country: new FormControl({value: '', disabled: true}, Validators.required)

【讨论】:

  • 不需要使用构造函数,只要 - 如果我们有一个带有 country 属性的对象数据 - 一些类似:country:[{data? data.country:'',disabled:true},Validators.required]
猜你喜欢
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多