내가 angular2 상당히 새로 온 사람과 지난 몇 일 동안 나는 모델 구동 형태재사용 구성 요소가
을 사용하여 재사용 가능한 형태로 구성 요소를 만들려고 한그래서 우리는 구성 요소가 말할 수 componentA.component.ts
@Component({
selector: 'common-a',
template: `
<div [formGroup]="_metadataIdentifier">
<div class="form-group">
<label>Common A[1]</label>
<div>
<input type="text" formControlName="valueA1">
<small>Description 1</small>
</div>
<div class="form-group">
<label>Common A[2]</label>
<div>
<input type="text" formControlName="valueA2">
<small>Description 2</small>
</div>
</div>
`
})
export class ComponentA implements OnInit{
@Input('group')
public myForm: FormGroup;
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.myForm = this._fb.group({
valueA1 : ['', [Validators.required]],
valueA2 : ['', [Validators.required]],
});
}
}
그리고 구성 요소 B는 componentB.component.ts
@Component({
selector: 'common-b',
template: `
<div [formGroup]="_metadataIdentifier">
<div class="form-group">
<label>Common B</label>
<div>
<input type="text" formControlName="valueB">
<small>Description</small>
</div>
</div>
`
})
export class ComponentB implements OnInit{
@Input('group')
public myForm: FormGroup;
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.myForm= this._fb.group({
valueB : ['', [Validators.required]]
});
}
}
내 질문은 캘리포니아 방법입니다 n 입력 제어를 주 구성 요소로 옮기지 않고이 두 하위 구성 요소를 사용하여 양식을 작성합니다. 예를 들어이 아이디어 뒤에 main.component.ts
@Component({
selector: 'main',
template: `
<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
<div>
<common-a [group]="formA"></common-a>
<common-b [group]="formB"></common-b>
<div>
<button>Register!</button>
</div>
</div>
</form>
`
})
export class Main implements OnInit{
@Input('group')
public myForm: FormGroup;
public formA : FormGroup;
public formB : FormGroup;
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.myForm = this._fb.group({
//how can I compose these two sub forms here
//leaving the form control names agnostic to this component
});
}
}
개념은 자신의 빌딩 블록의 일부를 공유하는 많은 복잡한 형태를 구축하는 것입니다. 이다
, 나는 [valueB
, valueA1
, valueA2
] 내 Main
구성 요소가 formControlNames
의 이름을 알고 싶어하지만, 자동적으로이를 삽입 및 업데이트/최상위 폼 그룹에서 유효성을 검사하지 않습니다.
올바른 방향에 대한 아이디어 나 요령이 도움이 될 것입니다.
감사합니다 많이, 이것은 정말로 도움이되었고 눈을 열었습니다 :) – stevengatsios
각도에 익숙하지 않고 모델 중심 양식으로 즉시 시작되었습니다. 호기심에서, * FormGroup * 필드를 권장 접근법이나 해결 방법으로 공유하는 방법은 무엇입니까? 복잡한 시스템을 구축하고 있으며 작은 구성 요소를 개발하고 부모 - 자식 관계 (4-5 단계)로 다시 사용하려고합니다. – Raf