[英]Binding json - formgroup
I have a form and I want to display data that comes from an API (JSon file) in mode readonly. 我有一个表单,我想以模式readonly显示来自API(JSon文件)的数据。 Do you have tracks?
你有曲目吗? I take an example of a form: Thank you
我举一个表格的例子:谢谢
<div class="col-sm-8 col-sm-offset-2">
<form [formGroup]="userForm" (ngSubmit)="onSubmitForm()">
<div class="form-group">
<label for="firstName">Prénom</label>
<input type="text" id="firstName" class="form-control" formControlName="firstName">
</div>
<div class="form-group">
<label for="lastName">Nom</label>
<input type="text" id="lastName" class="form-control" formControlName="lastName">
</div>
<div class="form-group">
<label for="email">Adresse e-mail</label>
<input type="text" id="email" class="form-control" formControlName="email">
</div>
<div class="form-group">
<label for="email">Adresse address</label>
<input type="text" id="address" class="form-control" formControlName="address">
</div>
<button type="submit" class="btn btn-primary">Soumettre</button>
</form>
</div>
File JSON 文件JSON
User:
- User1:
firstName: toto
lastName: titi
email: toto@toto.com
address: 2 rue titi
In your component (.ts file), as soon as the data comes back (typically inside of your ngOnInit), you should be able to do this.userForm.setValue(data)
. 在您的组件(.ts文件)中,一旦数据返回(通常在您的ngOnInit内),您应该能够执行此操作
this.userForm.setValue(data)
。
As for making them readonly, an easy way I know is to set the FormGroup as disabled when you create it. 至于使它们只读,我知道的一种简单方法是在创建时将FormGroup设置为禁用。 So
userForm = new FormGroup({_property_names_}, { disabled: true })
. 所以
userForm = new FormGroup({_property_names_}, { disabled: true })
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.