Angular 反应形式
Angular Reactive Form
我是 Angular 5 的新手。我目前正在研究 Angular 响应式表单。我有一个下面的 JSON 结构,我需要在从 FORM 获取值后 post 返回到 REST API。
JSON结构:
{
"empDesg": "Sr Developer",
"empID": 123,
"empName": "Sam",
"empSkills": [
"Java",
"Devops"
]
}
我设法将 empID、empName 和 empDesg 映射到表单控件;它们都将成为输入文本元素。我想使用 formcontrols 或 formarray 将 empSkills 映射到 Checkboxes(但不确定使用哪一个)- 只有我卡住了。
我的 HTML 和组件 class:
addEmp.component.html
<form class="form-emp" [formGroup]="empForm">
<div class="form-group row ">
<label for="empID" class="col-sm-2 col-form-label">EmpID</label>
<div class="col-sm-10">
<input type="text" formControlName="empID" name="empID" class="form-control" id="empID" placeholder="Employee ID">
<div *ngIf="empForm.controls.empID.invalid && empForm.controls.empID.touched">
<ngb-alert type="danger" [dismissible]="false">Employee ID is must</ngb-alert>
</div>
</div>
</div>
<div class="form-group row">
<label for="empName" class="col-sm-2 col-form-label">EmpName</label>
<div class="col-sm-10">
<input type="text" formControlName="empName" name="empName" class="form-control" id="empName" placeholder="Employee Name">
</div>
</div>
<div class="form-group row">
<label for="empDesgn" class="col-sm-2 col-form-label">Emp Title</label>
<div class="col-sm-10">
<input type="text" formControlName="empDesg" name="empDesg" class="form-control" id="empDesgn" placeholder="Employee Title">
</div>
</div>
<p>Form value: {{ empForm.value | json }}</p>
<div class="form-group row">
<div class="col-sm-2">Skillset</div>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Java
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Dot Net
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Dev Ops
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Business Analyst
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Automation Testing
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
UX
</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary" (click)="diag()">Add Employee</button>
</div>
</div>
</form>
addEmpComponent.ts
import { Component, OnInit } from '@angular/core';
import { Employee } from '../Employee'
import { FormBuilder, FormGroup, FormArray, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app-addemployee',
templateUrl: './addemployee.component.html',
styleUrls: ['./addemployee.component.css']
})
export class AddemployeeComponent implements OnInit {
empForm: FormGroup;
constructor(private fb: FormBuilder) {
this.createForm();
}
createForm() {
this.empForm = this.fb.group({
empID: ['', Validators.required],
empName: ['', Validators.required],
empDesg: ['', Validators.required]
});
}
model = new Employee();
ngOnInit() {
}
}
能否分享您关于如何在我上面的 JSON 结构中将复选框与 skillSet 数组映射的输入。如果您与我分享一段有用的代码。
非常感谢。
您可以查看 Reactive 表单 in an app which I have created to play around with it。此应用程序还包含您迟早会遇到的其他功能。
skillList: any[] = ['Java',Dot Net','Dev Ops'];
createForm() {
this.empForm = this.fb.group({
empID: ['', Validators.required],
empName: ['', Validators.required],
empDesg: ['', Validators.required],
skills: this.fb.array(['Java','Devops']),
});
}
isSkillChecked(data) {
return this.rForm.controls['skills'].value.includes(data);
}
html
<label>Skill Set:
<span *ngFor="let skill of skillList">
<input type="checkbox"
(change)="onSkillChange(skill,$event.target.checked)"
[checked]="isSkillChecked(skill)" /> {{skill}}
</span>
</label>
我已修改我的示例以满足您的需要。看一看告诉我。
home.component.html
<div>
<p>Form 1</p>
<form [formGroup]="registerForm">
<div *ngFor="let grpdata of statusdata">
<input type="checkbox" formControlName="title" value="{{grpdata.groupid}}" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.title.errors }">{{grpdata.groupname}}<br>
</div>
<div *ngIf="submitted && f.title.errors" class="invalid-feedback">
<div *ngIf="f.title.errors.required">Title is required</div>
</div>
<button type="submit" (click)="getSelectedVal()">Click here</button>
</form>
</div>
<div>
<p>Form 2</p>
<form [formGroup]="editForm">
<input type="textbox" disabled formControlName="edithidden" [(ngModel)]="hello" class="form-control"><br>
<div *ngFor="let grpdata of statusdata">
<input type="checkbox" formControlName="edittitle" value="{{grpdata.groupid}}" class="form-control" [ngClass]="{ 'is-invalid': submitted1 && g.edittitle.errors }">{{grpdata.groupname}}<br>
</div>
<div *ngIf="submitted1 && g.edittitle.errors" class="invalid-feedback">
<div *ngIf="g.edittitle.errors.required">Title is required</div>
</div>
<button type="submit" (click)="editSelectedVal()">Click here</button>
</form>
</div>
home.component.ts
import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
declare var $: any;
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
submitted = false;
submitted1 = false;
getListData: any;
registerForm: FormGroup;
editForm: FormGroup;
statusdata: any;
constructor(private commonserviceService: CommonserviceService,private formBuilder: FormBuilder)
{
this.registerForm = this.formBuilder.group({
title: ['', Validators.required],
});
this.editForm = this.formBuilder.group({
edittitle: ['', Validators.required],
edithidden: new FormControl()
});
}
ngOnInit() {
this.statusdata = [{"groupid":1,"groupname":"project1"},{"groupid":2,"groupname":"project2"},{"groupid":3,"groupname":"project3"}];
}
get f() { return this.registerForm.controls; }
get g() { return this.editForm.controls; }
getSelectedVal(){
this.submitted = true;
// stop here if form is invalid
if (this.registerForm.invalid) {
return;
}
console.log('submitted');
}
editSelectedVal(){
this.submitted1 = true;
// stop here if form is invalid
if (this.editForm.invalid) {
return;
}
console.log('submitted edit');
}
}
我是 Angular 5 的新手。我目前正在研究 Angular 响应式表单。我有一个下面的 JSON 结构,我需要在从 FORM 获取值后 post 返回到 REST API。
JSON结构:
{
"empDesg": "Sr Developer",
"empID": 123,
"empName": "Sam",
"empSkills": [
"Java",
"Devops"
]
}
我设法将 empID、empName 和 empDesg 映射到表单控件;它们都将成为输入文本元素。我想使用 formcontrols 或 formarray 将 empSkills 映射到 Checkboxes(但不确定使用哪一个)- 只有我卡住了。
我的 HTML 和组件 class:
addEmp.component.html
<form class="form-emp" [formGroup]="empForm">
<div class="form-group row ">
<label for="empID" class="col-sm-2 col-form-label">EmpID</label>
<div class="col-sm-10">
<input type="text" formControlName="empID" name="empID" class="form-control" id="empID" placeholder="Employee ID">
<div *ngIf="empForm.controls.empID.invalid && empForm.controls.empID.touched">
<ngb-alert type="danger" [dismissible]="false">Employee ID is must</ngb-alert>
</div>
</div>
</div>
<div class="form-group row">
<label for="empName" class="col-sm-2 col-form-label">EmpName</label>
<div class="col-sm-10">
<input type="text" formControlName="empName" name="empName" class="form-control" id="empName" placeholder="Employee Name">
</div>
</div>
<div class="form-group row">
<label for="empDesgn" class="col-sm-2 col-form-label">Emp Title</label>
<div class="col-sm-10">
<input type="text" formControlName="empDesg" name="empDesg" class="form-control" id="empDesgn" placeholder="Employee Title">
</div>
</div>
<p>Form value: {{ empForm.value | json }}</p>
<div class="form-group row">
<div class="col-sm-2">Skillset</div>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Java
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Dot Net
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Dev Ops
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Business Analyst
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Automation Testing
</label>
</div>
<div class="form-check">
<input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
UX
</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary" (click)="diag()">Add Employee</button>
</div>
</div>
</form>
addEmpComponent.ts
import { Component, OnInit } from '@angular/core';
import { Employee } from '../Employee'
import { FormBuilder, FormGroup, FormArray, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app-addemployee',
templateUrl: './addemployee.component.html',
styleUrls: ['./addemployee.component.css']
})
export class AddemployeeComponent implements OnInit {
empForm: FormGroup;
constructor(private fb: FormBuilder) {
this.createForm();
}
createForm() {
this.empForm = this.fb.group({
empID: ['', Validators.required],
empName: ['', Validators.required],
empDesg: ['', Validators.required]
});
}
model = new Employee();
ngOnInit() {
}
}
能否分享您关于如何在我上面的 JSON 结构中将复选框与 skillSet 数组映射的输入。如果您与我分享一段有用的代码。 非常感谢。
您可以查看 Reactive 表单 in an app which I have created to play around with it。此应用程序还包含您迟早会遇到的其他功能。
skillList: any[] = ['Java',Dot Net','Dev Ops'];
createForm() {
this.empForm = this.fb.group({
empID: ['', Validators.required],
empName: ['', Validators.required],
empDesg: ['', Validators.required],
skills: this.fb.array(['Java','Devops']),
});
}
isSkillChecked(data) {
return this.rForm.controls['skills'].value.includes(data);
}
html
<label>Skill Set:
<span *ngFor="let skill of skillList">
<input type="checkbox"
(change)="onSkillChange(skill,$event.target.checked)"
[checked]="isSkillChecked(skill)" /> {{skill}}
</span>
</label>
我已修改我的示例以满足您的需要。看一看告诉我。
home.component.html
<div>
<p>Form 1</p>
<form [formGroup]="registerForm">
<div *ngFor="let grpdata of statusdata">
<input type="checkbox" formControlName="title" value="{{grpdata.groupid}}" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.title.errors }">{{grpdata.groupname}}<br>
</div>
<div *ngIf="submitted && f.title.errors" class="invalid-feedback">
<div *ngIf="f.title.errors.required">Title is required</div>
</div>
<button type="submit" (click)="getSelectedVal()">Click here</button>
</form>
</div>
<div>
<p>Form 2</p>
<form [formGroup]="editForm">
<input type="textbox" disabled formControlName="edithidden" [(ngModel)]="hello" class="form-control"><br>
<div *ngFor="let grpdata of statusdata">
<input type="checkbox" formControlName="edittitle" value="{{grpdata.groupid}}" class="form-control" [ngClass]="{ 'is-invalid': submitted1 && g.edittitle.errors }">{{grpdata.groupname}}<br>
</div>
<div *ngIf="submitted1 && g.edittitle.errors" class="invalid-feedback">
<div *ngIf="g.edittitle.errors.required">Title is required</div>
</div>
<button type="submit" (click)="editSelectedVal()">Click here</button>
</form>
</div>
home.component.ts
import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
declare var $: any;
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
submitted = false;
submitted1 = false;
getListData: any;
registerForm: FormGroup;
editForm: FormGroup;
statusdata: any;
constructor(private commonserviceService: CommonserviceService,private formBuilder: FormBuilder)
{
this.registerForm = this.formBuilder.group({
title: ['', Validators.required],
});
this.editForm = this.formBuilder.group({
edittitle: ['', Validators.required],
edithidden: new FormControl()
});
}
ngOnInit() {
this.statusdata = [{"groupid":1,"groupname":"project1"},{"groupid":2,"groupname":"project2"},{"groupid":3,"groupname":"project3"}];
}
get f() { return this.registerForm.controls; }
get g() { return this.editForm.controls; }
getSelectedVal(){
this.submitted = true;
// stop here if form is invalid
if (this.registerForm.invalid) {
return;
}
console.log('submitted');
}
editSelectedVal(){
this.submitted1 = true;
// stop here if form is invalid
if (this.editForm.invalid) {
return;
}
console.log('submitted edit');
}
}