我已经创建了一个组件,该组件使用Angular material 表示 Select 框.此组件接受FormGroup作为输入,并在放置在另一个组件中时正常工作.我需要启用此组件的添加和删除使用按钮.我已经设法使用ViewContainerRef.createComponent使其工作,并可以传递动态填充SELECT内容所需的参数.但是,我遇到错误‘Error Error:找不到名为’Control2‘的控件.’
Parent component个
import { STEPPER_GLOBAL_OPTIONS } from "@angular/cdk/stepper";
import { Component, OnInit, ViewChild, ViewContainerRef } from "@angular/core";
import { FormBuilder, FormControl, FormGroup } from "@angular/forms";
@Component({
selector: "gcr-create",
template: `<gcr-select-form
[formGroup]="form"
formControlName="control1"
otherParam="OK"
></gcr-select-form>
</div>
<ng-template #containerMoreSelect></ng-template>
<button mat-stroked-button (click)="click()">Add More</button>`,
styles: [``],
providers: [
{
provide: STEPPER_GLOBAL_OPTIONS,
useValue: { showError: true },
},
],
})
export class ParentComponent implements OnInit {
@ViewChild("containerMoreSelect", {
read: ViewContainerRef,
})
containerMoreSelect!: ViewContainerRef;
public form!: FormGroup;
constructor() {}
ngOnInit(): void {
this.formInit();
}
formInit() {
this.form = new FormGroup({
control1: new FormControl(),
});
}
click() {
this.form.addControl("control2", new FormControl());
const component = this.containerMoreSelect.createComponent(SelectReteComponent);
component.setInput("formGroup", this.form);
component.setInput("formControlName", "control2");
component.setInput("otherParam", "OK");
}
}
Child component个
import {
Component,
EventEmitter,
Input,
OnChanges,
Output,
SimpleChanges,
} from "@angular/core";
import {
AbstractControl,
ControlContainer,
FormGroup,
FormGroupDirective,
} from "@angular/forms";
import { MatSelectChange } from "@angular/material/select";
import { ValueText } from "../models/value-text.model";
@Component({
selector: "gcr-select-form",
template: `<mat-form-field>
<mat-label>Select what you prefer</mat-label>
<mat-select
[formControlName]="formControlName"
(selectionChange)="getSelection($event)">
<mat-option></mat-option>
<mat-option *ngFor="let value of values" [value]="value.id">
{{ value.text}}
</mat-option>
</mat-select>
</mat-form-field>`,
styles: ``,
viewProviders: [
{
provide: ControlContainer,
useExisting: FormGroupDirective,
},
],
})
export class SelectReteComponent implements OnChanges {
@Input() formGroup!: FormGroup;
@Input() formControlName!: string;
@Input() otherParam!: string;
@Output() selected= new EventEmitter<ValueText>();
constructor(private ValuesService: ValuesRepositoryService) {}
/* Data are loaded from DB */
ngOnChanges(changes: SimpleChanges) {
/* Other stuff */
console.log("formGroup" + this.formGroup.controls); // I can see all control, control2 included
console.log("formControlName " + this.formControlName); // It shows control2 correctly
console.log("otherParam" + this.otherParam); // It shows "OK"
}
getSelection(event: MatSelectChange) {
console.log("event.value " + event.value);
this.selected.emit({
value: event.value,
text: event.source.triggerValue,
});
}
}
先谢谢你, 文森佐
我试图通过传递FormGroup
来加载子组件,我希望FormControl
能够被识别.