angular 同页面不同表单间的数据添加

本文介绍了如何在Angular应用中使用表单实现同页面内不同表单间的数据添加。通过创建FormGroup和使用FormControl,实现了表单字段的验证和数据绑定。当点击添加按钮时,将一个表单的数据添加到另一个表单的数据列表中,展示了Angular组件间数据交互的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.html文件:

<form nz-form [formGroup]="validateForm" class="form" (ngSubmit)="submitForm()">

<nz-table #basicTable nzBordered>

<tr>

<td width="300" style="text-align: right">规则名:</td>

<td>

<nz-form-item style="width: 400px;margin: 0">

<nz-form-control>

<nz-input-group>

<input type="text" nz-input formControlName="regularName" >

</nz-input-group>

<nz-form-explain *ngIf="validateForm.get('regularName').dirty && validateForm.get('regularName').errors">必填项!</nz-form-explain>

</nz-form-control>

</nz-form-item>

</td>

</tr>

<tr>

<td style="text-align: right">匹配方式:</td>

<td style="text-align: left">

<nz-select style="width: 200px;" [(ngModel)]="singleValue" [nzSize]="size" formControlName="matchmode">

<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>

</nz-select>

</td>

</tr>

<tr>

<td style="text-align: right">匹配项:</td>

<td>

<nz-form-item style="width: 400px;margin: 0">

<nz-form-control>

<nz-input-group>

<input type="text" nz-input formControlName="matchitem" style="width: 400px">

</nz-input-group>

<nz-form-explain *ngIf="validateForm.get('matchitem').dirty && validateForm.get('matchitem').errors">必填项!</nz-form-explain>

</nz-form-control>

</nz-form-item>

</td>

</tr>

<tr>

<td style="text-align: right">配置:</td>

<td style="text-align: left">

<label nz-checkbox [(ngModel)]="jinyong" formControlName="forbidden" value="1">禁用</label>

<label nz-checkbox [(ngModel)]="baojing" formControlName="alert">报警</label>

</td>

</tr>

<tr style="text-align: center">

<td colspan="2">

<button nz-button nzType="primary" (click)="add()">添加</button>

<button nz-button nzType="primary" (click)="remove()">移除</button>

</td>

</tr>

<tr>

<td style="text-align: right">已添加的黑名单:</td>

<td style="text-align: left">

<div>

<nz-table #secondTable nzBordered [nzData]="datas" style="">

<tr>

<td>规则名</td>

<td>匹配方式</td>

<td>匹配项</td>

<td>配置</td>

</tr>

<tr *ngFor="let data of secondTable.data">

<td>{{data.name}}</td>

<td>{{data.mode}}</td>

<td>{{data.item}}</td>

<td>{{data.bidden}}</td>

</tr>

</nz-table>

</div>

</td>

</tr>

</nz-table>

</form>

 

.ts文件:

import { Component, OnInit } from '@angular/core';

import {

AbstractControl,

FormBuilder,

FormGroup,

Validators

} from '@angular/forms';

 

@Component({

selector: 'service',

templateUrl: './service.component.html',

styleUrls: ['./service.component.css']

})

export class ServiceComponent implements OnInit {

validateForm: FormGroup;

secondTable: FormGroup;

accountItem:any = {};

formData:any= {};

jinyong = true;

baojing = true;

listOfOption = [];

size = 'default';

singleValue = '服务名称';

multipleValue = [ '服务名称', '服务的显示名称', '服务的执行路径' ];

tagValue = [ '服务名称', '服务的显示名称', '服务的执行路径' ];

datas=[];

dataSet:any = {};

constructor(private fb: FormBuilder) { }

ngOnInit() {

this.validateForm = this.fb.group({

regularName: [ null, [ Validators.required ] ],

matchitem: [ null, [ Validators.required ] ],

matchmode: [ null, [ Validators.required ] ],

alert: [ null, [ Validators.required ] ],

forbidden: [ null, [ Validators.required ] ],

 

});

const children = [];

for (let i = 0; i < 4; i++) {

children.push({ label: this.multipleValue[i], value: this.tagValue[i]});

}

this.listOfOption = children;


 

}

 

//添加(主要的方法)

add(){

this.dataSet.name = this.validateForm.controls.regularName.value;

this.dataSet.item = this.validateForm.controls.matchitem.value;

this.dataSet.mode = this.validateForm.controls.matchmode.value;

this.dataSet.bidden = this.validateForm.controls.forbidden.value;

this.datas.push(this.dataSet);

this.datas = JSON.parse(JSON.stringify(this.datas));

console.log(this.datas);

}

submitForm(){

for (const i in this.validateForm.controls) {

this.validateForm.controls[ i ].markAsDirty();

this.validateForm.controls[ i ].updateValueAndValidity();

}

}

 

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值