【问题标题】:angular 2 on from submit error self.context.onSubmit is not a functionangular 2 on from submit error self.context.onSubmit is not a function
【发布时间】: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


【解决方案1】:

当事件中调用的方法名称与模板声明和类内部不匹配时,会发生此错误

在您的模板中,您已将 onSubmit() 指定为驼峰式

<form class="ui form" (ngSubmit)="**onSubmit()**" #registrationForm="ngForm">

但在班级内部,它不是驼峰式“onsubmit()”

 onsubmit(){
        console.log('form submit clicked..');
        this._registerService.sendUser(this.newuser).subscribe(

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 2017-01-07
    • 2018-12-08
    • 1970-01-01
    • 2019-11-10
    • 2022-08-12
    • 2022-11-20
    • 2019-01-16
    • 1970-01-01
    相关资源
    最近更新 更多