我有一个asp.net核心/ Angular项目。 在我的app.module.client.ts文件中,我导入了' @ angular / forms'如下:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { sharedConfig } from './app.module.shared';
@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
BrowserModule,
FormsModule,
HttpModule,
...sharedConfig.imports
],
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin }
]
})
export class AppModule {
}

我的组件配置如下:
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Operator } from '../shared/operator.model';
@Component({
selector: 'operator-form',
template: require('./operator-form.component.html')
})
export class OperatorFormComponent {
model = new Operator("Login", "Name", "Last name", "password");
submitted = false;
onSubmit() { this.submitted = true; }
// TODO: Remove this when we're done
get diagnostic() { return JSON.stringify(this.model); }
}

我的模板配置如下:
<form #formOperator="ngForm">
<div class="form-group">
<label for="login">Login</label>
<input type="text" class="form-control" id="login" required [(ngModel)]="model.login" name="login" />
</div>
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName" required [(ngModel)]="model.firstName" name="firstName" />
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName" required [(ngModel)]="model.lastName" name="lastName" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="text" class="form-control" id="password" required [(ngModel)]="model.password" name="password" />
</div>
<div class="form-group">
<label for="expirationDate">Valid until</label>
<input type="date" class="form-control" id="expirationDate" [(ngModel)]="model.validUntil" name="expirationDate" />
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
&#13;
不幸的是我发现错误:&#34; exportAs&#34;没有指令。设置为&#34; ngForm&#34;
有什么想法吗?
谢谢!
答案 0 :(得分:0)
使用命令“dotnet new Angular”创建角度项目时,app.module分为三个文件:app.module.client.ts,app.module.server.ts和app.module.shared .TS。
为了正确导入包'@ angular / forms',您必须将其添加到app.module.shared.ts中,或者将其添加到客户端和服务器文件中。