gpt4 book ai didi

angular5 - 没有将 “exportAs” 设置为 “ngForm” 的指令

转载 作者:行者123 更新时间:2023-12-03 09:03:07 27 4
gpt4 key购买 nike

我已经包含了 FormsModule 但它显示错误。

There is no directive with “exportAs” set to “ngForm” 

我的 Login.component.ts :-

import { Component } from '@angular/core';
import { Http, Response , RequestOptions , Headers , URLSearchParams } from '@angular/http';
import {FormsModule} from '@angular/forms';

@Component({
templateUrl: './login.component.html'
})

export class LoginComponent {
apiRoot : String = "http://httpbin.org";
// search code starts here

// data = string;
constructor(private http: Http){

}
onSubmit(value){

let name = value.name;
let password = value.password;
let url = `${this.apiRoot}/post`;
this.http.post(url, {name:name,password:password}).subscribe(res => console.log(res.json()));
console.log('submit');

}


}

我的login.module.ts:-

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { jqxGridComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxgrid';
import { AppComponent } from './../../app.component';
import { FormsModule, ReactiveFormsModule , FormGroup} from '@angular/forms';
import { HttpModule } from '@angular/http';
import { LoginComponent } from './login.component';

@NgModule({
declarations: [
AppComponent, jqxGridComponent, LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ReactiveFormsModule

],
providers: [],
bootstrap: [AppComponent]
})
export class LoginModule { }

我的login.component.html

<div class="">
<form (ngSubmit)="onSubmit(heroForm.value)" #heroForm="ngForm">
<input type="text" name="name" [(ngModel)]="name" />
<input type="password" name="password" [(ngModel)]="password" />
<br/>
<input type="checkbox" name="vehicle" value="Bike" [(ngModel)]="vehicle"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car" > I have a car<br>
<button type="submit">Send</button>
</form>
</div>

我尝试制作新的 Angular 项目,它适用于主 app.module.ts、app.component.ts 和 app.component.html 文件,但当我尝试使用登录文件时,它不起作用。

我是 Angular-5 的新手,所以当我们创建新的登录模块时,我可能遗漏了一些东西。

编辑:-

应用程序组件:-

app.component.ts 

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
// tslint:disable-next-line
selector: 'body',
template: '<router-outlet></router-outlet>'
})
export class AppComponent implements OnInit {
constructor(private router: Router) { }

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
// Import containers
import {
FullLayoutComponent,
SimpleLayoutComponent
} from './containers';

const APP_CONTAINERS = [
FullLayoutComponent,
SimpleLayoutComponent
]

// Import components
import {
AppAsideComponent,
AppBreadcrumbsComponent,
AppFooterComponent,
AppHeaderComponent,
AppSidebarComponent,
AppSidebarFooterComponent,
AppSidebarFormComponent,
AppSidebarHeaderComponent,
AppSidebarMinimizerComponent,
APP_SIDEBAR_NAV
} from './components';

const APP_COMPONENTS = [
AppAsideComponent,
AppBreadcrumbsComponent,
AppFooterComponent,
AppHeaderComponent,
AppSidebarComponent,
AppSidebarFooterComponent,
AppSidebarFormComponent,
AppSidebarHeaderComponent,
AppSidebarMinimizerComponent,
APP_SIDEBAR_NAV
]

// Import directives
import {
AsideToggleDirective,
NAV_DROPDOWN_DIRECTIVES,
ReplaceDirective,
SIDEBAR_TOGGLE_DIRECTIVES
} from './directives';

const APP_DIRECTIVES = [
AsideToggleDirective,
NAV_DROPDOWN_DIRECTIVES,
ReplaceDirective,
SIDEBAR_TOGGLE_DIRECTIVES
]

// Import routing module
import { AppRoutingModule } from './app.routing';

// Import 3rd party components
import { ChartsModule } from 'ng2-charts/ng2-charts';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { ModalModule } from 'ngx-bootstrap';
import { TabsModule } from 'ngx-bootstrap/tabs';

@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
ChartsModule,
HttpModule,
FormsModule,
ReactiveFormsModule,
BsDropdownModule.forRoot(),
ModalModule.forRoot(),
TabsModule.forRoot(),
],
declarations: [
AppComponent,
...APP_CONTAINERS,
...APP_COMPONENTS,
...APP_DIRECTIVES
],
providers: [{
provide: LocationStrategy,
useClass: HashLocationStrategy
}],
bootstrap: [ AppComponent ]
})
export class AppModule { }

我的登录页面是我在 View 文件夹下创建的文件。来自 app 的路径。

views/pages/login.component.html View /页面/login.component.ts View /页面/login.module.ts

最佳答案

Angular 6/7/8 出现同样的错误,只需删除 #
即可解决heroForm="ngForm 可以使用

关于angular5 - 没有将 “exportAs” 设置为 “ngForm” 的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48983713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com