如何使用 FormBuilder 使用初始值 Validators.Required 和 disabled: true 初始化表单控件?

Mau*_*ice 0 angular angular-reactive-forms

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}
  })
});
Run Code Online (Sandbox Code Playgroud)

PeS*_*PeS 9

是的,它确实有这样的构造函数

country: new FormControl({value: '', disabled: true}, Validators.required)
Run Code Online (Sandbox Code Playgroud)

  • 不需要使用构造函数,只要 - 如果我们有一个带有属性 country 的对象数据 - 就像: country:[{data? data.country:'',disabled:true},Validators.required] (2认同)