[英]Angular.JS losing form HTML when I add [(ngModel)]
I am trying to create a simple form with validations in Angular.JS. 我试图在Angular.JS中创建一个带有验证的简单表单。 Just learning how to use the framework.
只是学习如何使用框架。 When I add [(ngModel)] to my first form input, I lose all the HTML.
当我在第一个表单输入中添加[(ngModel)]时,我丢失了所有HTML。 When I take it out [(ngModel)], all the html returns.
当我将其取出[[ngModel)]时,所有html都会返回。 Here is my code:
这是我的代码:
app/user.ts 应用/用户
export class User {
constructor(
public firstname: string = "",
public lastname: string = "",
public email: string = "",
public password: string = "",
public address: string = "",
public unitaptnumber: string = "",
public city: string = "",
public state: string = "",
public luck: string = "",
){}
}
app/app.component.ts app / app.component.ts
import { Component } from '@angular/core';
import { User } from './user';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
user = new User();
users = [];
onSubmit(){
this.users.push(this.user);
this.user = new User();
}
}
app/app.component.html app / app.component.html
<h1>Registration</h1>
<h3>Account Information</h3>
<form (submit)="onSubmit()">
First Name <input
type="text"
name="firstname"
required
[(ngModel)]="user.firstname"
#firstname="ngModel"
>
<input type="submit" value="Register">
</form>
app.module.ts app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
What is creating this problem? 是什么造成了这个问题?
The forms module needs to be imported and provided at the module level, not at the component level. 表单模块需要在模块级别而不是组件级别导入并提供。 Your app module should look like:
您的应用模块应如下所示:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
I believe you encountered a syntax error whenever you add [(ngModel)]="user.firstname"
due to user.firstname
being undefined. 我相信每当添加
[(ngModel)]="user.firstname"
时都会遇到语法错误,因为user.firstname
未定义。
first of all change the way you declare these public variables: 首先,更改声明这些公共变量的方式:
export class User {
public firstname: string = "";
public lastname: string = "";
public email: string = "";
public password: string = "";
public address: string = "";
public unitaptnumber: string = "";
public city: string = "";
public state: string = "";
public luck: string = "";
}
second your User
declaration should be changed too: 其次,您的
User
声明也应更改:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent
{
// make this of type User
user: User;
users: any;
constructor()
{
// init code goes here always
this.user = new User();
this.users = [];
}
onSubmit(){
this.users.push(this.user);
this.user = new User();
}
}
hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.