本文介绍了ng-submit事件在Angular 2中被两次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Angular 2中发生ng-submit事件时,该方法被调用两次.如何仅使调用方法一次?为什么方法在角度2中调用两次?
The method is called twice When the ng-submit event occurring in Angular 2. How do I make a call method only once? Why does method calls twice in the angular 2?
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-form-ngform',
template: `
<form (ngSubmit)="onSubmit()">
<button type="submit" class="btn btn-default">Submit</button>
</form>
`
})
export class FormNgformComponent {
onSubmit() {
alert('test');
}
}
推荐答案
可能与此重复:
确保您正在像这样引导应用程序:
Make sure you are bootstrapping your app like this:
import {disableDeprecatedForms, provideForms} from '@angular/forms';
bootstrap(AppComponent, [
disableDeprecatedForms(),
provideForms()
]);
这篇关于ng-submit事件在Angular 2中被两次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!