英文:
Issue with login and registration in angular?
问题
Angular CLI: 9.0.0-rc.7
Angular: 9.0.0-rc.7
I'm working with login but get an error:
browser console error:
formGroup expects a FormGroup instance. Please pass one in.
<div [formGroup]="myGroup">
<input formControlName="firstName">
</div>
In your class:
this.myGroup = new FormGroup({
firstName: new FormControl()
});
login.component.html
login.component.ts
import { Component, OnInit, OnChanges, Input } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormsModule, FormControl, ReactiveFormsModule, Validators } from "@angular/forms";
export class LoginComponent {
logincreteform : FormGroup;
constructor(private router: Router) {
}
ngOnInit() {
this.logincreteform = new FormGroup({
username: new FormControl(),
password: new FormControl()
});
}
onSubmit() {
console.log(this.logincreteform);
let username;
let password ;
if (username == 'abc' && password == '123') {
this.router.navigate(['/register']);
}
else {
console.log("error");
}
}
}
app.module.ts
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { LoginComponent } from './views/login/login.component';
import { RegisterComponent } from './views/register/register.component';
@NgModule({
imports: [
ReactiveFormsModule,
declarations: [
LoginComponent,
RegisterComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
and when I uncomment register page code:
then also give an error:
compiler.js:2531 Uncaught Error: Template parse errors:
No provider for ControlContainer (
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论