我有三个选项卡,默认情况下,第一个选项卡应该打开带有表单的详细信息。我使用角度 io 文档
https://angular.io/guide/dynamic-form中给出的示例动态生成每个选项卡表单
下面是模板
<div class="tab-content">
<form [formGroup]="myform" novalidate (submit)="onSubmit()">
<div [ngClass]="{'dynamic-field-inline': feild.isTwoElementInTheRow}" class="form-group" *ngFor="let feild of templateData[dataIndex].details">
<label [attr.for]="feild.name">{{feild.label}}</label>
<div [ngSwitch]="feild.type">
<input *ngSwitchCase="'textbox'" class="form-control" [formControlName]="feild.key" [id]="feild.key" [type]="feild.type">
<select class="form-control" [id]="feild.key" *ngSwitchCase="'dropdown'" [formControlName]="feild.key">
<option *ngFor="let opt of feild.options" [value]="opt.key">{{opt.value}}</option>
</select>
<textarea *ngSwitchCase="'textarea'" class="form-control" [formControlName]="feild.key" rows="5" [id]="feild.key"></textarea>
</div>
<!--<div class="errorMessage" *ngIf="!myform.controls[feild.key].valid">{{feild.label}} is required</div>-->
</div>
<div class="form-row">
<button type="submit" [disabled]="!myform.valid">Save</button>
</div>
</form>
</div>
下面是打字稿代码
let fields = [{
"key": "brave",
"type": "textbox",
"label": "Bravery Rating",
"required": true,
"readable": true,
"isTwoElementInTheRow": false
},
{
"key": "brave1",
"label": "Bravery Rating",
"type": "dropdown",
"isTwoElementInTheRow": false,
"options": [{
"key": "solid",
"value": "Solid"
},
{
"key": "great",
"value": "Great"
},
{
"key": "good",
"value": "Good"
},
{
"key": "unproven",
"value": "Unproven"
}
]
},
{
"key": "brave2",
"label": "Bravery Rating",
"type": "textarea"
},
{
"key": "brave3",
"type": "textbox",
"label": "Bravery Rating"
}
];
let group: any = {};
fields.forEach(field => {
group[field.key] = field.required ? new FormControl(field.value || '', Validators.required)
: new FormControl(field.value || '');
});
this.myForm = new FormGroup(group);
现在,当我点击 url 时,我收到一条错误消息
CreateTemplateComponent.html:31 ERROR Error: Cannot find control with name: 'brave'
at _throwError (forms.js:2432)
at setUpControl (forms.js:2300)
at FormGroupDirective.addControl (forms.js:6664)
at FormControlName._setUpControl (forms.js:7314)
at FormControlName.ngOnChanges (forms.js:7227)
at checkAndUpdateDirectiveInline (core.js:12358)
at checkAndUpdateNodeInline (core.js:13886)
at checkAndUpdateNode (core.js:13829)
at debugCheckAndUpdateNode (core.js:14722)
at debugCheckDirectivesFn (core.js:14663)
但是当我切换标签时它正在工作。谁能帮帮我吗。