如何使用 Angular 响应式表单在父组件中添加子组件?
How to add a child component within parent component using Angular Reactive forms?
我正在向父组件表单添加子组件。
Pessoa.html
<form [formGroup]="usuarioForm" novalidate>
<div class="col-md-4">
<div class="form-group">
<label for="exampleInputNome">Nome</label>
<input type="text"
formControlName="nome"
class="form-control "
placeholder="Nome" />
</div>
</div>
<endereco [enderecoGroup]=usuarioForm></endereco>
<button type="submit"
class="btn btn-default"
(click)="InsereUsuario(usuarioForm.value)">
Enviar
</button>
</form>
Endereco.html
<h4>Endereço</h4>
<div class="col-md-2">
<div class="form-group" [formGroup]="enderecoGroup">
<label for="exampleInputCep">Cep</label>
<input type="text"
class="form-control"
placeholder="Cep"
(blur)="GetAdress($event.target.value)">
</div>
</div>
<div class="col-md-4">
<div class="form-group" [formGroup]="enderecoGroup">
<label for="exampleInputComplemento">Complemento</label>
<input type="text"
class="form-control"
placeholder="Complemento"
[value]=endereco?.complemento>
</div>
</div>
Endereco.ts
@Input() enderecoGroup: FormGroup;
endereco: Endereco
GetEndereco(Cep: string) {
this.servico.GetEndereco(Cep)
.subscribe(resposta => this.endereco = resposta);
}
提交表单时 Adress.html 条目为空,我该怎么办?
我正在使用 React 表单
Stackblitz
阅读关于 React Forms 的 Angular 文档,我发现:
使用 setValue()
and patchValue()
填充表单模型
我正在使用 patchValue()
:
Endereco.ts
GetEndereco(Cep: string) {
this.servico.GetEndereco(Cep)
.subscribe(response => {
this.enderecoGroup.patchValue({
bairro: response.bairro,
logradouro: response.logradouro,
cidade: response.localidade
});
});
}
Demo stackblitz
打印:
我以我认为更好的方式做到了。为地址创建内部组并将其传递给地址组件。
.TS
this.usuarioForm = this.formBuilder.group({
nome: this.formBuilder.control('', [Validators.required, Validators.minLength(3)]),
sobreNome: this.formBuilder.control('', [Validators.required]),
emailAdress: this.formBuilder.control('', [Validators.required, Validators.email]),
tipoPessoa: this.formBuilder.control('1', [Validators.required]),
endereco: this.formBuilder.group({ // Another form group inside the bigger group for better encapsulation.
cep: this.formBuilder.control('', [Validators.minLength(8), Validators.maxLength(9)]),
logradouro: this.formBuilder.control('', [Validators.required]),
bairro: this.formBuilder.control('', [Validators.required]),
cidade: this.formBuilder.control('', [Validators.required]),
complemento: ''
})
});
get endereco() {
return this.usuarioForm.get('endereco') as FormGroup; // this is just a refence to the inside group that contains information just relavant to address
}
Html:
<endereco [enderecoGroup]=endereco></endereco> //pass just the inside formGroup
这样就不需要监听子组件的变化了
我正在向父组件表单添加子组件。
Pessoa.html
<form [formGroup]="usuarioForm" novalidate>
<div class="col-md-4">
<div class="form-group">
<label for="exampleInputNome">Nome</label>
<input type="text"
formControlName="nome"
class="form-control "
placeholder="Nome" />
</div>
</div>
<endereco [enderecoGroup]=usuarioForm></endereco>
<button type="submit"
class="btn btn-default"
(click)="InsereUsuario(usuarioForm.value)">
Enviar
</button>
</form>
Endereco.html
<h4>Endereço</h4>
<div class="col-md-2">
<div class="form-group" [formGroup]="enderecoGroup">
<label for="exampleInputCep">Cep</label>
<input type="text"
class="form-control"
placeholder="Cep"
(blur)="GetAdress($event.target.value)">
</div>
</div>
<div class="col-md-4">
<div class="form-group" [formGroup]="enderecoGroup">
<label for="exampleInputComplemento">Complemento</label>
<input type="text"
class="form-control"
placeholder="Complemento"
[value]=endereco?.complemento>
</div>
</div>
Endereco.ts
@Input() enderecoGroup: FormGroup;
endereco: Endereco
GetEndereco(Cep: string) {
this.servico.GetEndereco(Cep)
.subscribe(resposta => this.endereco = resposta);
}
提交表单时 Adress.html 条目为空,我该怎么办?
我正在使用 React 表单
Stackblitz
阅读关于 React Forms 的 Angular 文档,我发现:
使用 setValue()
and patchValue()
我正在使用 patchValue()
:
Endereco.ts
GetEndereco(Cep: string) {
this.servico.GetEndereco(Cep)
.subscribe(response => {
this.enderecoGroup.patchValue({
bairro: response.bairro,
logradouro: response.logradouro,
cidade: response.localidade
});
});
}
Demo stackblitz
打印:
我以我认为更好的方式做到了。为地址创建内部组并将其传递给地址组件。
.TS
this.usuarioForm = this.formBuilder.group({
nome: this.formBuilder.control('', [Validators.required, Validators.minLength(3)]),
sobreNome: this.formBuilder.control('', [Validators.required]),
emailAdress: this.formBuilder.control('', [Validators.required, Validators.email]),
tipoPessoa: this.formBuilder.control('1', [Validators.required]),
endereco: this.formBuilder.group({ // Another form group inside the bigger group for better encapsulation.
cep: this.formBuilder.control('', [Validators.minLength(8), Validators.maxLength(9)]),
logradouro: this.formBuilder.control('', [Validators.required]),
bairro: this.formBuilder.control('', [Validators.required]),
cidade: this.formBuilder.control('', [Validators.required]),
complemento: ''
})
});
get endereco() {
return this.usuarioForm.get('endereco') as FormGroup; // this is just a refence to the inside group that contains information just relavant to address
}
Html:
<endereco [enderecoGroup]=endereco></endereco> //pass just the inside formGroup
这样就不需要监听子组件的变化了