Angular 5 次提交未触发
Angular 5 submit not firing
我目前正在学习 Angular 并且正在学习一些教程,我创建了一个简单的注册组件:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
form: FormGroup;
createForm() {
this.form = this.formBuilder.group({
email: '',
username: '',
password: '',
confirm: ''
})
}
onRegisterSubmit() {
console.log('this.form');
}
constructor(private formBuilder: FormBuilder) {
this.createForm();
}
ngOnInit() {
}
}
并在 html 中定义了以下形式:
<h1 class="page-header">Registration Page</h1>
<form [formGroup]="form" ng-submit="onRegisterSubmit()">
<div class="form-group">
<label for="username">Username</label>
<div>
<input type="text" name="username" class="form-control" autocomplete="off" placeholder="*Username" formControlName="username" />
</div>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<div>
<input type="text" name="email" class="form-control" autocomplete="off" placeholder="*Email" formControlName="email" />
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<div>
<input type="text" name="password" class="form-control" autocomplete="off" placeholder="*Password" formControlName="password" />
</div>
</div>
<div class="form-group">
<label for="confirm">Confirm Password</label>
<div>
<input type="text" name="confirm" class="form-control" autocomplete="off" placeholder="*Confirm Password" formControlName="confirm" />
</div>
</div>
<input type="submit" class="btn btn-primary" value="submit" />
</form>
我似乎无法触发 onRegisterSubmit
函数。
我已尝试对表单使用不同的事件((submit)
、(ng-submit)
、ng-submit
)并调试表单,但我看不到任何事件触发点击提交。
谁能看出我做错了什么?
尝试(ngSubmit)="onRegisterSubmit()"
要达到预期结果,请使用 ngSubmit
根据 angular 文档 - ngSubmit
"You can listen to the directive's ngSubmit event to be notified when the user has triggered a form submission. The ngSubmit event will be emitted with the original form submission event."
查看此 link 了解更多详情 - https://angular.io/api/forms/NgForm
我目前正在学习 Angular 并且正在学习一些教程,我创建了一个简单的注册组件:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
form: FormGroup;
createForm() {
this.form = this.formBuilder.group({
email: '',
username: '',
password: '',
confirm: ''
})
}
onRegisterSubmit() {
console.log('this.form');
}
constructor(private formBuilder: FormBuilder) {
this.createForm();
}
ngOnInit() {
}
}
并在 html 中定义了以下形式:
<h1 class="page-header">Registration Page</h1>
<form [formGroup]="form" ng-submit="onRegisterSubmit()">
<div class="form-group">
<label for="username">Username</label>
<div>
<input type="text" name="username" class="form-control" autocomplete="off" placeholder="*Username" formControlName="username" />
</div>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<div>
<input type="text" name="email" class="form-control" autocomplete="off" placeholder="*Email" formControlName="email" />
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<div>
<input type="text" name="password" class="form-control" autocomplete="off" placeholder="*Password" formControlName="password" />
</div>
</div>
<div class="form-group">
<label for="confirm">Confirm Password</label>
<div>
<input type="text" name="confirm" class="form-control" autocomplete="off" placeholder="*Confirm Password" formControlName="confirm" />
</div>
</div>
<input type="submit" class="btn btn-primary" value="submit" />
</form>
我似乎无法触发 onRegisterSubmit
函数。
我已尝试对表单使用不同的事件((submit)
、(ng-submit)
、ng-submit
)并调试表单,但我看不到任何事件触发点击提交。
谁能看出我做错了什么?
尝试(ngSubmit)="onRegisterSubmit()"
要达到预期结果,请使用 ngSubmit
根据 angular 文档 - ngSubmit
"You can listen to the directive's ngSubmit event to be notified when the user has triggered a form submission. The ngSubmit event will be emitted with the original form submission event."
查看此 link 了解更多详情 - https://angular.io/api/forms/NgForm