【发布时间】:2019-01-30 22:39:25
【问题描述】:
我有一个独立于主AppModule 的ClientModule 模块。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ClientsRoutingModule } from './clients-routing.module';
import { ClientsListComponent } from './clients-list/clients-list.component';
import { ClientAddComponent } from './client-add/client-add.component';
import { ClientDetailComponent } from './client-detail/client-detail.component';
@NgModule({
imports: [
CommonModule,
ClientsRoutingModule
],
declarations: [ClientsListComponent, ClientAddComponent, ClientDetailComponent]
})
export class ClientsModule { }
我在AppModule 上导入了FormsModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我正在尝试将输入字段从视图绑定到以下组件:
import { Component, OnInit, Input } from '@angular/core';
import { ClientModel } from '../client.model';
@Component({
selector: 'app-client-add',
templateUrl: './client-add.component.html',
styleUrls: ['./client-add.component.scss']
})
export class ClientAddComponent implements OnInit {
fname: string;
lname: string;
constructor() { }
ngOnInit() {
}
createClient() {
console.log(this.fname, this.lname);
}
}
以下是我的观点
<div class="card-body">
<form class="form">
<div class="form-group">
<label for="">First Name {{fname}}</label>
<input type="text" ([ngModel]) = "fname" class="form-control rounded-0" required>
</div>
<div class="form-group">
<label for="">Last Name</label>
<input type="text" ([ngModel]) = "lname" class="form-control rounded-0">
</div>
<button type="button" (click)="createClient()" class="btn btn-success float-center">Create New Client</button>
</form>
</div>
当我单击Create New Client 按钮时,生成的日志是undefined undefined
我究竟做错了什么?我花了很多时间来解决我的代码中的这个问题/错误,但到目前为止它还不好。请帮忙。
这可能是无关的,但我遵循了关于模块延迟加载的 angular.io 指南,其中提到我应该在网络选项卡中发生延迟加载时看到传入的 chunk 文件,但我没有。或许与此有关?
编辑
我还尝试在ClientsModule 中导入FormsModule。问题仍然存在。
编辑
但是,[(ngModel)] 在 app.component.ts 中的 AppModule 中按预期工作。
【问题讨论】:
标签: data-binding angular6 angular-components ngmodel