0
我收到了截圖中提供的下面的錯誤。我在角4Angular 4錯誤:formGroup需要一個FormGroup實例
創建登錄表單下面是當前的代碼有錯誤。
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { ProductComponent } from './product/product.component';
import { MembersComponent } from './members/members.component';
import { SortPipe } from './app.sortpipe';
@NgModule({
declarations: [
SortPipe,
AppComponent,
ProductComponent,
MembersComponent
],
imports: [
BrowserModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
HTML
<form class="container" [formGroup]="myGroup" (ngSubmit)="onSubmit(myGroup.value)">
<input type="text" name="firstName" placeholder="firstName" formControlName="firstName"/>
<br/><br/>
<input type="text" name="lastName" placeholder="lastName" formControlName="lastName"/>
<br/><br/>
<select name="gender" formControlName="gender">
<option value="female">female</option>
<option value="male">male</option>
</select>
<br/><br/>
<input type="submit" value="submit">
</form>
<br/><br/><br/>
app.component.ts
import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
onSubmit= function (user){
console.log("user",user);
}
myGroup ;
ngOnInIt(){
this.myGroup = new FormGroup({
firstName : new FormControl("umair"),
lastName : new FormControl(""),
gender : new FormControl(""),
})
}
}
任何幫助/指着我在正確的方向將是偉大的!謝謝!
請考慮在問題主體中更詳細地記錄錯誤,而不是留給回答者來打開截圖。 – Forty3