gpt4 book ai didi

angular - 复选框值不会以 react 形式改变 Angular

转载 作者:行者123 更新时间:2023-12-05 05:10:16 27 4
gpt4 key购买 nike

我在响应式表单的帮助下从 json 文件生成表单。表单的生成工作正常,但我面临的问题是 - 当我第一次在 mozila 中单击复选框时,它的值变为“打开”。但是当我取消选中它时,什么也没有发生,它的值仍然是“On”,应该是“off or false”。在 chrome 中点击什么都没有发生。

应用程序组件.ts

import { Component } from '@angular/core';
import { QuestionControlService } from './question-control.service';
import GxData from './gx.json';
@Component({
selector: 'app-root',
template: `
<div>
<app-dynamic-form [questions]="questions"></app-dynamic-form>
</div>
`,
providers: []
})
export class AppComponent{

questions: any[];
constructor() {
this.questions = GxData;
}

}

动态表单.components.ts

import { Component, Input, OnInit, SimpleChanges } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { QuestionControlService } from '../question-control.service';
import SbbData from '../sbb.json';
import GxData from '../gx.json';

@Component({
selector: 'app-dynamic-form',
templateUrl: './dynamic-form.component.html',
providers: [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {

@Input() questions: any[] = [];
form: FormGroup;
payLoad = '';

constructor(private qcs: QuestionControlService) { }

ngOnInit() {
this.form = this.qcs.toFormGroup(this.questions);
}
callGx() {
this.questions = GxData;
this.form = this.qcs.toFormGroup(this.questions);
}
callSbb() {
this.questions = SbbData;
this.form = this.qcs.toFormGroup(this.questions);
}

onSubmit() {

this.payLoad = JSON.stringify(this.form.value);
console.log(JSON.parse(this.payLoad));
}

}

动态表单.component.html

<div class="search_box">
<form (ngSubmit)="onSubmit()" [formGroup]="form">
<button type="button" (click)="callSbb()">SBB</button>
<button type="button" (click)="callGx()">GX</button>
<div *ngFor="let question of questions" class="form-row">
<app-question [question]="question" [form]="form"></app-question>
</div>

<div class="form-row">
<button type="submit" [disabled]="!form.valid">Save</button>
</div>
</form>

<div *ngIf="payLoad" class="form-row">
<strong>Saved the following values</strong><br>{{payLoad}}
</div>
</div>
{{form.value |json}}

问题控制.service.ts

import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Injectable()
export class QuestionControlService {
constructor() { }

toFormGroup(questions: any[]) {
let group: any = {};

questions.forEach(question => {
group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
: new FormControl(question.value || '');
});
return new FormGroup(group);
}
}

动态表单问题.component.ts

import { Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';

// import { QuestionBase } from '../question-base';

@Component({
selector: 'app-question',
templateUrl: './dynamic-form-question.component.html'
})
export class DynamicFormQuestionComponent {
@Input() question: any;
@Input() form: FormGroup;
get isValid() { return this.form.controls[this.question.key].valid; }
}

动态表单问题.component.html

<div [formGroup]="form">


<div [ngSwitch]="question.controlType" class="checkbox_wrapper">

<input *ngSwitchCase="'textbox'" [formControlName]="question.key" [id]="question.key" [type]="question.type" name="{{question.name}}">
<label [attr.for]="question.key">{{question.label}}</label>

<select [id]="question.key" *ngSwitchCase="'dropdown'" [formControlName]="question.key" name = "{{question.name}}" >
<option *ngFor="let opt of question.options" [attr.value]="opt.key" [selected]="opt.select">{{opt.value}}</option>
</select>
</div>
<div class="errorMessage" *ngIf="!isValid">{{question.label}} is required</div>
</div>

两个 json 文件看起来像

[
{
"key": "Context",
"label": "Context",
"required": false,
"order": 1,
"controlType": "textbox",
"name": "Context",
"type": "checkbox"
},
{
"key": "contextopt",
"label": "",
"required": false,
"order": 2,
"controlType": "dropdown",
"name": "contextopt",
"options": [
{
"key": "All Context",
"value": "All Context",
"select": true
},
{
"key": "aaa",
"value": "aaa"
},
{
"key": "bb",
"value": "bb"
},
{
"key": "Other",
"value": "Other"
}
]
},
{
"key": "Movement",
"label": "Movement",
"required": false,
"order": 3,
"controlType": "textbox",
"name": "Movement",
"type": "checkbox"
}
]

最佳答案

更新 问题出在您编写 [type]="question.type" 和 question.type="checkbox"时。看起来 Angular 不喜欢这个用于复选框,(但喜欢使用电子邮件、数字......)

ng*SwitchCase="'textbox'" 替换为

<ng-container *ngSwitchCase="'textbox'">
<input *ngIf="question.type!='checkbox'" [formControlName]="question.key"
[id]="question.key" [type]="question.type">
<input *ngIf="question.type=='checkbox'" [formControlName]="question.key"
[id]="question.key" type="checkbox">
</ng-container>

您可以成为 stackblitz 中的榜样

其他的想法是创建一个新的类型,“checkbox”和一个新的 switchCase 选项新的 CheckBoxQuestion 是文本框的副本

export class CheckBoxQuestion extends QuestionBase<boolean> {
controlType = 'checkbox';

constructor(options: {} = {}) {
super(options);
}
}

和 switchCase 添加

<input *ngSwitchCase="'checkbox'" type="checkbox" [formControlName]="question.key">

,在 stackblitz 中你有两种情况

注意:您正在创建一个大循环

<div [formGroup]="form">
<input formGroupName="question.key">
</div>
<div [formGroup]="form">
<input formGroupName="question.key">
</div>
...

我建议,将您的 dynamic-form-question.component.html 更改为简单地创建

<div>
<input [formControl]="form.get(question.key)" ..>
</div>

是的,将组合 [formGroup]="form"..formControlName=".."更改为 [formControl]="form.get(..")。

关于angular - 复选框值不会以 react 形式改变 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56792721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com