英文:
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 (
Register
Create your account<"): ng:///AppModule/RegisterComponent.html@7:14
register.component.html
<div class="card-body p-4">
<form>
<h1>Register</h1>
<p class="text-muted">Create your account</p>
<div class="form-group">
<input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
</div>
<div class="form-group">
<input type="text" formControlName="email" class="form-control" placeholder="Email" autocomplete="email" required>
</div>
<div class="form-group">
<input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="new-password" required>
</div>
<button type="button" class="btn btn-block btn-success">Create Account</button>
</div>
register.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: 'register.component.html'
})
export class RegisterComponent {
constructor() { }
}
I want the simple login to enter username(abc) and password(123) then redirecting to the registration page?? what I'm doing wrong? help.
英文:
angular version:
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
<div class="card-body">
<form [formGroup]="logincreteform" (ngSubmit)="onSubmit()">
<div class="form-group">
<input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
</div>
<div class="form-group">
<input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="current-password" required>
</div>
<button type="submit" class="btn btn-primary px-4">Login</button>
</form>
</div>
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 ("
<div class="card mx-4">
<div class="card-body p-4">
[ERROR ->]<form>
<h1>Register</h1>
<p class="text-muted">Create your account<"): ng:///AppModule/RegisterComponent.html@7:14
register.component.html
<div class="card-body p-4">
<form>
<h1>Register</h1>
<p class="text-muted">Create your account</p>
<div class="form-group">
<input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
</div>
<div class="form-group">
<input type="text" formControlName="email" class="form-control" placeholder="Email" autocomplete="email" required>
</div>
<div class="form-group">
<input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="new-password" required>
</div>
<button type="button" class="btn btn-block btn-success">Create Account</button>
</div>
register.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: 'register.component.html'
})
export class RegisterComponent {
constructor() { }
}
I want the simple login to enter username(abc) and password(123) then redirecting to the registration page?? what I m doing wrong?help.
答案1
得分: 1
你在注册组件中缺少了formGroup。你需要在form标签中添加它,就像在登录组件中所做的一样。
register.component.html
<div class="card-body p-4">
<form [formGroup]="registerform" (ngSubmit)="onSubmit()">
<h1>Register</h1>
<p class="text-muted">Create your account</p>
<div class="form-group">
<input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
</div>
<div class="form-group">
<input type="text" formControlName="email" class="form-control" placeholder="Email" autocomplete="email" required>
</div>
<div class="form-group">
<input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="new-password" required>
</div>
<button type="button" class="btn btn-block btn-success">Create Account</button>
</form>
</div>
你还需要在你的component.ts文件中导入FormGroup并添加它。
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; // 请确保导入了正确的模块
@Component({
selector: 'app-dashboard',
templateUrl: 'register.component.html'
})
export class RegisterComponent {
registerform: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.registerform = this.formBuilder.group({
username: ['', Validators.required],
email: ['', Validators.required],
password: ['', Validators.required]
});
}
onSubmit() {
// 处理表单提交逻辑
}
}
英文:
You are missing formGroup in the register component. You need to add it in form tag the same as you did it in login component.
register.component.html
<div class="card-body p-4">
<form [formGroup]="registerform" (ngSubmit)="onSubmit()>
<h1>Register</h1>
<p class="text-muted">Create your account</p>
<div class="form-group">
<input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
</div>
<div class="form-group">
<input type="text" formControlName="email" class="form-control" placeholder="Email" autocomplete="email" required>
</div>
<div class="form-group">
<input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="new-password" required>
</div>
<button type="button" class="btn btn-block btn-success">Create Account</button>
</div>
you need add formGroup in you component.ts file as well.
`import { Component } from '@angular/core';
import { FormsModule,ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-dashboard',
templateUrl: 'register.component.html'
})
export class RegisterComponent {
registerform : FormGroup;
constructor() { }
}`
答案2
得分: 0
现在我的问题已解决。
login.component.cs
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, Validators, FormBuilder } from "@angular/forms";
@Component({
selector: 'app-dashboard',
templateUrl: 'login.component.html'
})
export class LoginComponent {
login: FormGroup;
constructor(private frmbuilder: FormBuilder, public router: Router) {
this.login = this.frmbuilder.group({
username: [''],
password: ['']
});
}
onSubmit() {
console.log(this.login);
let username = this.login.get('username').value;
let password = this.login.get('password').value;
if (username == 'abc' && password == '123') {
this.router.navigate(['/dashboard']); // 当您的条件匹配时导航到仪表板页面,否则提供错误。(导航)
}
else {
console.log("error");
}
}
Do Not Forget this line in Html page
<form [formGroup]="login" (ngSubmit)="onSubmit()">
<!-- 不要忘记在HTML页面中包含以下行 -->
<form [formGroup]="login" (ngSubmit)="onSubmit()">
英文:
now my issue is Solved.
login.component.cs
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, Validators, FormBuilder} from "@angular/forms";
@Component({
selector: 'app-dashboard',
templateUrl: 'login.component.html'
})
export class LoginComponent {
login: FormGroup;
constructor(private frmbuilder: FormBuilder, public router:Router) {
this.login = this.frmbuilder.group({
username: [''],
password: ['']
});
}
onSubmit() {
console.log(this.login);
let username = this.login.get('username').value;
let password = this.login.get('password').value;
if (username == 'abc' && password == '123') {
this.router.navigate(['/dashboard']); //navigate when your criteria match then redirect dashboard page otherwise give an error.(Navigation)
}
else {
console.log("error");
}
}
Do Not Forget this line in Html page
<form [formGroup]="login" (ngSubmit)="onSubmit()">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论