【发布时间】:2016-11-17 14:14:48
【问题描述】:
我在 Angular 2 应用程序中使用 2.0.0-rc.6。 在提交表单时出现此错误 - self.context.onSubmit 不是函数
它还在浏览器中附加表单值。
http://localhost:3000/register
在提交页面重新加载和url变成这样。
http://localhost:3000/register?firstName=vcvvc&lastName=vcv&userName=cvv&password=vcv&password=vcv
代码是
表格
<form class="ui form" (ngSubmit)="onSubmit()" #registrationForm="ngForm">
----
----
<button type="submit" class="ui button"> Register</button>
</form>
服务
import { Component } from '@angular/core';
import { User } from '../models/user';
import { RegisterService } from '../services/register.service';
@Component({
selector: 'side-panel',
templateUrl: 'app/components/register.component.html'
})
export class RegisterComponent {
newuser: User = new User();
theText: string;
constructor(private _registerService: RegisterService){
}
onsubmit(){
console.log('form submit clicked..');
this._registerService.sendUser(this.newuser).subscribe(
date =>{
this.newuser = new User();
},
error => console.log(error)
);
}
}
【问题讨论】:
-
记住 javascript 是区分大小写的。您的函数应重命名为
onSubmit而不是onsubmit。 -
哦,天哪,这是深夜编码的问题 :-)。谢谢哈利
-
对我来说同样的问题,但我调用了函数 ngSubmit 而不是 onSubmit!
标签: forms angular form-submit onsubmit