当我将以下代码(见下文)表格模板添加到我的Angualr应用程序中时,我在终端上收到错误'未知方法'onSubmit''。
我也在控制台中收到此错误,但我可以通过完全删除元素来清除它。
*
Uncaught Error: Cannot assign to a reference or variable!
at _AstToIrVisitor.visitPropertyWrite (compiler.js:26390)
at PropertyWrite.visit (compiler.js:4679)
at convertActionBinding (compiler.js:25814)
at eval (compiler.js:28414)
at Array.forEach (<anonymous>)
at ViewBuilder._createElementHandleEventFn (compiler.js:28410)
at nodes.(:4200/anonymous function) (webpack-internal:///./node_modules/@angular/compiler/esm5/compiler.js:28050:27)
at eval (compiler.js:28355)
at Array.map (<anonymous>)
at ViewBuilder._createNodeExpressions (compiler.js:28354)
*
有人能指出我正确的方向吗?
附加client.component.html
<div class="row">
<div class="col-md-6">
<a routerLink="/" href="#" class="btn btn-link"><i class="fa fa-arrow-circle-o-left"></i> Back to Dashboard</a>
</div>
<div class="col-md-6">
</div>
</div>
<div class="card">
<div class="card-header">
Add Client
</div>
<div class="card-block">
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<!-- <form #validateForm="ngForm" (ngSubmit)="validate(validateForm)"> -->
<div class="form-group">
<label for="firstName">First Name</label>
<input
type="text"
class="form-control"
[(ngModel)]="clientFirstName"
name="firstName"
#clientFirstName="ngModel"
>
<div *ngIf="clientFirstName?.error.required && clientFirstName.touched" class="alert alert-danger">
First Name is required
</div>
<div *ngIf="clientFirstName?.error.minlength && clientFirstName.touched" class="alert alert-danger">
Must be at least 2 characters
</div>
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input
type="text"
class="form-control"
[(ngModel)]="clientLastName"
name="lastName"
#clientLastName="ngModel"
minlength="2"
required
>
<div *ngIf="clientLastName?.error.required && clientLastName.touched" class="alert alert-danger">
Last Name is required
</div>
<div *ngIf="clientLastName?.error.minlength && clientLastName.touched" class="alert alert-danger">
Must be at least 2 characters
</div>
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input
type="number"
class="form-control"
[(ngModel)]="clientPhone"
name="phone"
#clientPhone="ngModel"
minlength="10"
>
<div *ngIf="clientPhone?.error.minlength && clientPhone.touched" class="alert alert-danger">
Add a valid Phone Number
</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input
type="text"
class="form-control"
[(ngModel)]="clientEmail"
name="email"
#clientEmail="ngModel"
minlength="2"
required
>
<div *ngIf="clientEmail?.error.required && clientEmail.touched" class="alert alert-danger">
Email is required
</div>
<div *ngIf="clientEmail?.error.minlength && clientEmail.touched" class="alert alert-danger">
Must be at least 2 characters
</div>
</div>
<div class="form-group">
<label for="balance">Balance</label>
<input
type="number"
class="form-control"
[(ngModel)]="clientBalance"
name="balance"
#clientBalance="ngModel"
[disabled]="disableBalanceOnAdd">
</div>
<button type="submit" class="btn btn-primary btn-block"></button>
</form>
</div>
</div>
add.client.component.ts
import { Component, OnInit } from '@angular/core';
import { Client } from '../../models/client';
import { FormsModule } from '@angular/forms';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-add-client',
templateUrl: './add-client.component.html',
styleUrls: ['./add-client.component.css']
})
export class AddClientComponent implements OnInit {
client: Client = {
firstName: '',
lastName: '',
email: '',
Phone: '',
balance: 0
}
disableBalanceOnAdd:boolean = true;
constructor() { }
ngOnInit() {
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';
import {NgForm} from '@angular/forms';
//AngularFire imports
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from "angularfire2/database-deprecated"
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireAuth } from 'angularfire2/auth';
//Component Imports
import { AppComponent } from './app.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { ClientsComponent } from './components/clients/clients.component';
import { ClientDetailsComponent } from './components/client-details/client-details.component';
import { AddClientComponent } from './components/add-client/add-client.component';
import { EditClientComponent } from './components/edit-client/edit-client.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { SidebarComponent } from './components/sidebar/sidebar.component';
import { LoginComponent } from './components/login/login.component';
import { RegisterComponent } from './components/register/register.component';
import { SettingsComponent } from './components/settings/settings.component';
import { PageNotFoundComponent } from './components/page-not-found/page-not-found.component';
//Service Imports
import { ClientsService } from './services/clients.service';
const appRoutes: Routes = [
{ path: '', component:DashboardComponent },
{ path: 'login', component: LoginComponent },
{ path: 'register' , component:RegisterComponent },
{ path: 'add-client' , component:AddClientComponent }
];
export const firebaseConfig = {
//all my firebase api requests (removing for S.O.)
}
@NgModule({
declarations: [
AppComponent,
DashboardComponent,
ClientsComponent,
ClientDetailsComponent,
AddClientComponent,
EditClientComponent,
NavbarComponent,
SidebarComponent,
LoginComponent,
RegisterComponent,
SettingsComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes),
AngularFireModule.initializeApp(firebaseConfig),
AngularFireDatabaseModule,
AngularFireAuthModule,
FormsModule
],
providers: [
AngularFireAuth,
ClientsService
],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 0 :(得分:1)
你的组件中应该有一个onSubmit()方法,因为代码的这一行$(window).on("resize", function(){
//Your code for adding/removing class
});
在组件中添加了这个:
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
此处表单将包含您的每个输入的所有值
答案 1 :(得分:0)
这是导致问题的一行:
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
您需要在组件中实现onSubmit
方法。
import { Component, OnInit } from '@angular/core';
import { Client } from '../../models/client';
import { FormsModule } from '@angular/forms';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-add-client',
templateUrl: './add-client.component.html',
styleUrls: ['./add-client.component.css']
})
export class AddClientComponent implements OnInit {
client: Client = {
firstName: '',
lastName: '',
email: '',
Phone: '',
balance: 0
}
disableBalanceOnAdd:boolean = true;
constructor() { }
ngOnInit() {}
onSubmit(val: any) {
// do something with the form result
}
}