gpt4 book ai didi

html - 在angular2中获取表单的所有值

转载 作者:太空狗 更新时间:2023-10-29 19:36:37 28 4
gpt4 key购买 nike

我可以使用以下代码获取表单的值:

<input type="text" formControlName="subscriptionFormName" id="subscriptionFormName" #UserName class="form-control" mdbInputDirective>
<button class="btn btn-indigo btn-lg btn-block waves-light" type="button" (click)="OnSubmit(UserName.value)" mdbWavesEffect>Send
<i class="fa fa-paper-plane-o ml-1"></i>
</button>

但我需要的是获取表单的所有值,这样我就可以这样做:

<button class="btn btn-indigo btn-lg btn-block waves-light" type="button" (click)="OnSubmit(allFormValues)" mdbWavesEffect>Send
<i class="fa fa-paper-plane-o ml-1"></i>
</button>

user: User;
ObSubmit(values) {
user.UserName = values.UserName;
user.Password = values.Password;
......
}

完整代码如下:

<div class="row">
<div class="col-md-12 text-left">

<form [formGroup]="subscriptionForm">
<h3 style="color: gray; text-align: center;">Registration</h3>

<div class="row">
<div class="col-md-6">
<div class="md-form">
<i class="fa fa-user prefix grey-text"></i>
<input type="text" formControlName="UserName" id="UserName" class="form-control" mdbInputDirective>
<label for="UserName">Your UserName</label>
</div>
<br>
<div class="md-form">
<i class="fa fa-user prefix grey-text"></i>
<input type="text" formControlName="FirstName" id="FirstName" class="form-control" mdbInputDirective>
<label for="FirstName">Your First name</label>
</div>
</div>
<div class="col-md-6">
<div class="md-form">
<i class="fa fa-user-secret prefix grey-text"></i>
<input type="password" id="Password" formControlName="Password" class="form-control" mdbInputDirective>
<label for="Password">Your password</label>
</div>
<br>
<div class="md-form">
<i class="fa fa-user-user prefix grey-text"></i>
<input type="text" id="LastName" formControlName="LastName" class="form-control" mdbInputDirective>
<label for="LastName">Your Last name</label>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<div class="md-form">
<i class="fa fa-envelope prefix grey-text"></i>
<input type="email" id="Email" formControlName="Email" class="form-control" mdbInputDirective>
<label for="Email">Your Email</label>
</div>
<br>
{{subscriptionForm.value|json}}
<br>
<div class="text-center">
<button class="btn btn-indigo btn-lg btn-block waves-light" type="button" (click)="OnSubmit()" mdbWavesEffect>Send
<i class="fa fa-paper-plane-o ml-1"></i>
</button>
</div>
</div>
</div>
</form>

</div>
</div>

import { User } from './../../shared/user.model';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
subscriptionForm: FormGroup;
user: User;

constructor(private fb: FormBuilder) {
this.subscriptionForm = fb.group({
UserName: ['', Validators.required],
Password: ['', Validators.required],
Email: ['', Validators.required, Validators.email],
FirstName: ['', Validators.required],
LastName: ['', Validators.required]
});
}

ngOnInit() {
}

OnSubmit() {
this.user = this.subscriptionForm.value;
console.log(this.user);
}

}

当我使用 #subscriptionForm="ngForm" 时出现错误。可能是因为 [formGroup]="subscriptionForm"。我不知道什么是 react 形式或其他形式。之后我会研究它,但你能帮我吗?谢谢。

最佳答案

已接受的答案已过时并且不再适用于 Angular 8+,这可以很好地获取您的所有值:

for (const field in this.myForm.controls) { // 'field' is a string
console.log(this.myForm.controls[field].value);
}

关于html - 在angular2中获取表单的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50132150/

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