以angular2模型驱动的形式重用组件

前端之家收集整理的这篇文章主要介绍了以angular2模型驱动的形式重用组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对angular2很新,过去几天我一直在尝试使用模型驱动的表单创建可重用的表单组件

所以我们假设我们有一个组件componentA.component.ts

  1. @Component({
  2. selector: 'common-a',template: `
  3. <div [formGroup]="_MetadataIdentifier">
  4. <div class="form-group">
  5. <label>Common A[1]</label>
  6. <div>
  7. <input type="text" formControlName="valueA1">
  8. <small>Description 1</small>
  9. </div>
  10. <div class="form-group">
  11. <label>Common A[2]</label>
  12. <div>
  13. <input type="text" formControlName="valueA2">
  14. <small>Description 2</small>
  15. </div>
  16. </div>
  17. `
  18. })
  19.  
  20.  
  21. export class ComponentA implements OnInit{
  22.  
  23. @Input('group')
  24. public myForm: FormGroup;
  25.  
  26. constructor(private _fb: FormBuilder) {
  27. }
  28.  
  29. ngOnInit() {
  30. this.myForm = this._fb.group({
  31. valueA1 : ['',[Validators.required]],valueA2 : ['',});
  32. }
  33. }

和组件B componentB.component.ts

  1. @Component({
  2. selector: 'common-b',template: `
  3. <div [formGroup]="_MetadataIdentifier">
  4. <div class="form-group">
  5. <label>Common B</label>
  6. <div>
  7. <input type="text" formControlName="valueB">
  8. <small>Description</small>
  9. </div>
  10. </div>
  11. `
  12. })
  13.  
  14.  
  15. export class ComponentB implements OnInit{
  16.  
  17. @Input('group')
  18. public myForm: FormGroup;
  19.  
  20. constructor(private _fb: FormBuilder) {
  21. }
  22.  
  23. ngOnInit() {
  24. this.myForm= this._fb.group({
  25. valueB : ['',[Validators.required]]
  26. });
  27. }
  28. }

我的问题是如何使用这两个子组件组成一个表单而不将输入控制移动到主组件.
例如main.component.ts

  1. @Component({
  2. selector: 'main',template: `
  3. <form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
  4. <div>
  5. <common-a [group]="formA"></common-a>
  6. <common-b [group]="formB"></common-b>
  7. <div>
  8. <button>Register!</button>
  9. </div>
  10. </div>
  11. </form>
  12. `
  13. })
  14.  
  15.  
  16. export class Main implements OnInit{
  17.  
  18. @Input('group')
  19. public myForm: FormGroup;
  20.  
  21. public formA : FormGroup;
  22.  
  23. public formB : FormGroup;
  24.  
  25. constructor(private _fb: FormBuilder) {
  26. }
  27.  
  28. ngOnInit() {
  29. this.myForm = this._fb.group({
  30. //how can I compose these two sub forms here
  31. //leaving the form control names agnostic to this component
  32. });
  33. }
  34. }

这个想法背后的概念是构建许多共享一些构建块的复杂形式.

也就是说,我不希望我的主要组件知道formControlNames [valueA1,valueA2,valueB]的名称,但是自动插入它们并在顶级表单组上更新/验证.

任何想法或指向正确方向都会有所帮助.

这可以通过将我们的顶级FormGroup传递给子组件并让子组件使用formGroupName将其自身添加到更高级别的FormGroup来实现,这种方式上层FormGroup基本上不需要知道更低级别:

main.component.ts

  1. template: `<...>
  2. <common-a [parentForm]="myForm"></common-a>
  3. <...>

我们还将删除formA,formB声明,因为它们不再使用.

component-a.component.ts [formGroup]是我们的父组,formGroupName是我们如何识别和附加组件的控件作为一个组来工作在更大的整体(它们将嵌套在父组内).

  1. @Component({<...>
  2. template: `
  3. <div [formGroup]="parentForm">
  4. <div class="form-group">
  5. <label>Common A[1]</label>
  6. <div formGroupName="componentAForm">
  7. <input type="text" formControlName="valueA1">
  8. <small>Description 1</small>
  9. </div>
  10. <div class="form-group">
  11. <label>Common A[2]</label>
  12. <div formGroupName="componentAForm">
  13. <input type="text" formControlName="valueA2">
  14. <small>Description 2</small>
  15. </div>
  16. </div>`
  17. })
  18.  
  19. export class ComponentA implements OnInit {
  20. @Input() parentForm: FormGroup;
  21. componentAForm: FormGroup;
  22.  
  23. constructor(private _fb: FormBuilder) {}
  24.  
  25. ngOnInit() {
  26. this.componentAForm = this._fb.group({
  27. valueA1: ['',Validators.required],valueA2: ['',Validators.required]
  28. });
  29.  
  30. this.parentForm.addControl("componentAForm",this.componentAForm);
  31. }
  32. }

这里有一个plunker(注意我在这里使组件B更加动态,看它是否可以完成,但上面的实现同样适用于B):http://plnkr.co/edit/fYP10D45pCqiCDPnZm0u?p=preview

猜你在找的Angularjs相关文章