我实现了一个简单的Angular-CLI模板表单,提交按钮没有触发,验证也无效。当我按下提交时,值没有显示在浏览器控制台中,因为我预期我提到了html和typescript。
html
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Dating App</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class ="nav navbar-nav">
<li><a href ="#">Matches</a></li>
<li><a href ="#">Lists</a></li>
<li><a href ="#">Messages</a></li>
</ul>
<form #loginForm ="ngForm" class="navbar-form navbar-right" (ngSubmit)="Login()">
<div class="form-group">
<input placeholder="Email" class="form-control" type="text" required [(ngModel)]="model.username" name ="username">
</div>
<div class="form-group">
<input placeholder="Password" class="form-control" type="password" required [(ngModel)]="model.password" name ="password">
</div>
<button type="submit" [disabled]= "!loginForm.valid" class="btn btn-success">Sign in</button>
</form>
</div><!--/.navbar-collapse -->
</div>
</nav>
typescript
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {
model: any ;
constructor() { }
ngOnInit() {
}
Login() {
console.log(this.model);
}
}
答案 0 :(得分:0)
我认为您的问题是您可能没有在app.module.ts中添加“FormsModule”。
在app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
FormsModule
],
declarations: [
AppComponent,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private model:any[]=[];
constructor(){
}
private Login() {
console.log(this.model);
}
}
这个解决方案对我有用。希望这有帮助。感谢。