关于Angular中的登录和注册问题?

huangapple go评论110阅读模式
英文:

Issue with login and registration in angular?

问题

  1. Angular CLI: 9.0.0-rc.7
  2. Angular: 9.0.0-rc.7
  3. I'm working with login but get an error:
  4. browser console error:
  5. formGroup expects a FormGroup instance. Please pass one in.
  6. <div [formGroup]="myGroup">
  7. <input formControlName="firstName">
  8. </div>
  9. In your class:
  10. this.myGroup = new FormGroup({
  11. firstName: new FormControl()
  12. });
  13. login.component.html
  1. <div class="form-group">
  2. <input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="current-password" required>
  3. </div>
  4. <button type="submit" class="btn btn-primary px-4">Login</button>
  5. </form>
  1. 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 {

  1. logincreteform : FormGroup;
  2. constructor(private router: Router) {
  3. }
  4. ngOnInit() {
  5. this.logincreteform = new FormGroup({
  6. username: new FormControl(),
  7. password: new FormControl()
  8. });
  9. }

onSubmit() {
console.log(this.logincreteform);
let username;
let password ;
if (username == 'abc' && password == '123') {
this.router.navigate(['/register']);
}
else {
console.log("error");
}
}
}

  1. 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 { }

  1. and when I uncomment register page code:
  2. then also give an error:

compiler.js:2531 Uncaught Error: Template parse errors:
No provider for ControlContainer (

[ERROR ->]

Register

Create your account<"): ng:///AppModule/RegisterComponent.html@7:14

  1. register.component.html
  1. <div class="card-body p-4">
  2. <form>
  3. <h1>Register</h1>
  4. <p class="text-muted">Create your account</p>
  5. <div class="form-group">
  6. <input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
  7. </div>
  8. <div class="form-group">
  9. <input type="text" formControlName="email" class="form-control" placeholder="Email" autocomplete="email" required>
  10. </div>
  11. <div class="form-group">
  12. <input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="new-password" required>
  13. </div>
  14. <button type="button" class="btn btn-block btn-success">Create Account</button>
  15. </div>
  1. register.component.ts

import { Component } from '@angular/core';

@Component({
selector: 'app-dashboard',
templateUrl: 'register.component.html'
})
export class RegisterComponent {

constructor() { }

}

  1. 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.

  1. &lt;div [formGroup]=&quot;myGroup&quot;&gt;
  2. &lt;input formControlName=&quot;firstName&quot;&gt;
  3. &lt;/div&gt;
  4. In your class:
  5. this.myGroup = new FormGroup({
  6. firstName: new FormControl()
  7. });

login.component.html

  1. &lt;div class=&quot;card-body&quot;&gt;
  2. &lt;form [formGroup]=&quot;logincreteform&quot; (ngSubmit)=&quot;onSubmit()&quot;&gt;
  3. &lt;div class=&quot;form-group&quot;&gt;
  4. &lt;input type=&quot;text&quot; formControlName=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;Username&quot; autocomplete=&quot;username&quot; required&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;form-group&quot;&gt;
  7. &lt;input type=&quot;password&quot; formControlName=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;Password&quot; autocomplete=&quot;current-password&quot; required&gt;
  8. &lt;/div&gt;
  9. &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary px-4&quot;&gt;Login&lt;/button&gt;
  10. &lt;/form&gt;
  11. &lt;/div&gt;

login.component.ts

  1. import { Component, OnInit,OnChanges, Input } from &#39;@angular/core&#39;;
  2. import { Router } from &#39;@angular/router&#39;;
  3. import { FormGroup, FormsModule, FormControl, ReactiveFormsModule, Validators} from &quot;@angular/forms&quot;;
  4. export class LoginComponent {
  5. logincreteform : FormGroup;
  6. constructor(private router: Router) {
  7. }
  8. ngoninit() {
  9. this.logincreteform = new FormGroup({
  10. username: new FormControl(),
  11. password: new FormControl()
  12. });
  13. }
  14. onSubmit() {
  15. console.log(this.logincreteform);
  16. let username;
  17. let password ;
  18. if (username == &#39;abc&#39; &amp;&amp; password == &#39;123&#39;) {
  19. this.router.navigate([&#39;/register&#39;]);
  20. }
  21. else {
  22. console.log(&quot;error&quot;);
  23. }
  24. }
  25. }

app.module.ts

  1. import { FormsModule,ReactiveFormsModule } from &#39;@angular/forms&#39;;
  2. import { LoginComponent } from &#39;./views/login/login.component&#39;;
  3. import { RegisterComponent } from &#39;./views/register/register.component&#39;;
  4. @NgModule({
  5. imports: [
  6. ReactiveFormsModule,
  7. declarations: [
  8. LoginComponent,
  9. RegisterComponent
  10. ],
  11. bootstrap: [ AppComponent ]
  12. })
  13. export class AppModule { }

and when I uncomment register page code:

then also give an error:

  1. compiler.js:2531 Uncaught Error: Template parse errors:
  2. No provider for ControlContainer (&quot;
  3. &lt;div class=&quot;card mx-4&quot;&gt;
  4. &lt;div class=&quot;card-body p-4&quot;&gt;
  5. [ERROR -&gt;]&lt;form&gt;
  6. &lt;h1&gt;Register&lt;/h1&gt;
  7. &lt;p class=&quot;text-muted&quot;&gt;Create your account&lt;&quot;): ng:///AppModule/RegisterComponent.html@7:14

register.component.html

  1. &lt;div class=&quot;card-body p-4&quot;&gt;
  2. &lt;form&gt;
  3. &lt;h1&gt;Register&lt;/h1&gt;
  4. &lt;p class=&quot;text-muted&quot;&gt;Create your account&lt;/p&gt;
  5. &lt;div class=&quot;form-group&quot;&gt;
  6. &lt;input type=&quot;text&quot; formControlName=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;Username&quot; autocomplete=&quot;username&quot; required&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;form-group&quot;&gt;
  9. &lt;input type=&quot;text&quot; formControlName=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Email&quot; autocomplete=&quot;email&quot; required&gt;
  10. &lt;/div&gt;
  11. &lt;div class=&quot;form-group&quot;&gt;
  12. &lt;input type=&quot;password&quot; formControlName=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;Password&quot; autocomplete=&quot;new-password&quot; required&gt;
  13. &lt;/div&gt;
  14. &lt;button type=&quot;button&quot; class=&quot;btn btn-block btn-success&quot;&gt;Create Account&lt;/button&gt;
  15. &lt;/div&gt;

register.component.ts

  1. import { Component } from &#39;@angular/core&#39;;
  2. @Component({
  3. selector: &#39;app-dashboard&#39;,
  4. templateUrl: &#39;register.component.html&#39;
  5. })
  6. export class RegisterComponent {
  7. constructor() { }
  8. }

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

  1. <div class="card-body p-4">
  2. <form [formGroup]="registerform" (ngSubmit)="onSubmit()">
  3. <h1>Register</h1>
  4. <p class="text-muted">Create your account</p>
  5. <div class="form-group">
  6. <input type="text" formControlName="username" class="form-control" placeholder="Username" autocomplete="username" required>
  7. </div>
  8. <div class="form-group">
  9. <input type="text" formControlName="email" class="form-control" placeholder="Email" autocomplete="email" required>
  10. </div>
  11. <div class="form-group">
  12. <input type="password" formControlName="password" class="form-control" placeholder="Password" autocomplete="new-password" required>
  13. </div>
  14. <button type="button" class="btn btn-block btn-success">Create Account</button>
  15. </form>
  16. </div>

你还需要在你的component.ts文件中导入FormGroup并添加它。

  1. import { Component } from '@angular/core';
  2. import { FormGroup, FormBuilder, Validators } from '@angular/forms'; // 请确保导入了正确的模块
  3. @Component({
  4. selector: 'app-dashboard',
  5. templateUrl: 'register.component.html'
  6. })
  7. export class RegisterComponent {
  8. registerform: FormGroup;
  9. constructor(private formBuilder: FormBuilder) {
  10. this.registerform = this.formBuilder.group({
  11. username: ['', Validators.required],
  12. email: ['', Validators.required],
  13. password: ['', Validators.required]
  14. });
  15. }
  16. onSubmit() {
  17. // 处理表单提交逻辑
  18. }
  19. }
英文:

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

  1. &lt;div class=&quot;card-body p-4&quot;&gt;
  2. &lt;form [formGroup]=&quot;registerform&quot; (ngSubmit)=&quot;onSubmit()&gt;
  3. &lt;h1&gt;Register&lt;/h1&gt;
  4. &lt;p class=&quot;text-muted&quot;&gt;Create your account&lt;/p&gt;
  5. &lt;div class=&quot;form-group&quot;&gt;
  6. &lt;input type=&quot;text&quot; formControlName=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;Username&quot; autocomplete=&quot;username&quot; required&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;form-group&quot;&gt;
  9. &lt;input type=&quot;text&quot; formControlName=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Email&quot; autocomplete=&quot;email&quot; required&gt;
  10. &lt;/div&gt;
  11. &lt;div class=&quot;form-group&quot;&gt;
  12. &lt;input type=&quot;password&quot; formControlName=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;Password&quot; autocomplete=&quot;new-password&quot; required&gt;
  13. &lt;/div&gt;
  14. &lt;button type=&quot;button&quot; class=&quot;btn btn-block btn-success&quot;&gt;Create Account&lt;/button&gt;
  15. &lt;/div&gt;

you need add formGroup in you component.ts file as well.

  1. `import { Component } from &#39;@angular/core&#39;;
  2. import { FormsModule,ReactiveFormsModule } from &#39;@angular/forms&#39;;
  3. @Component({
  4. selector: &#39;app-dashboard&#39;,
  5. templateUrl: &#39;register.component.html&#39;
  6. })
  7. export class RegisterComponent {
  8. registerform : FormGroup;
  9. constructor() { }
  10. }`

答案2

得分: 0

现在我的问题已解决。

  1. login.component.cs
  2. import { Component, OnInit } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import { FormGroup, FormControl, Validators, FormBuilder } from "@angular/forms";
  5. @Component({
  6. selector: 'app-dashboard',
  7. templateUrl: 'login.component.html'
  8. })
  9. export class LoginComponent {
  10. login: FormGroup;
  11. constructor(private frmbuilder: FormBuilder, public router: Router) {
  12. this.login = this.frmbuilder.group({
  13. username: [''],
  14. password: ['']
  15. });
  16. }
  17. onSubmit() {
  18. console.log(this.login);
  19. let username = this.login.get('username').value;
  20. let password = this.login.get('password').value;
  21. if (username == 'abc' && password == '123') {
  22. this.router.navigate(['/dashboard']); // 当您的条件匹配时导航到仪表板页面,否则提供错误。(导航)
  23. }
  24. else {
  25. console.log("error");
  26. }
  27. }
  28. Do Not Forget this line in Html page
  29. <form [formGroup]="login" (ngSubmit)="onSubmit()">
  1. <!-- 不要忘记在HTML页面中包含以下行 -->
  2. <form [formGroup]="login" (ngSubmit)="onSubmit()">
英文:

now my issue is Solved.

  1. login.component.cs
  2. import { Component, OnInit } from &#39;@angular/core&#39;;
  3. import { Router } from &#39;@angular/router&#39;;
  4. import { FormGroup, FormControl, Validators, FormBuilder} from &quot;@angular/forms&quot;;
  5. @Component({
  6. selector: &#39;app-dashboard&#39;,
  7. templateUrl: &#39;login.component.html&#39;
  8. })
  9. export class LoginComponent {
  10. login: FormGroup;
  11. constructor(private frmbuilder: FormBuilder, public router:Router) {
  12. this.login = this.frmbuilder.group({
  13. username: [&#39;&#39;],
  14. password: [&#39;&#39;]
  15. });
  16. }
  17. onSubmit() {
  18. console.log(this.login);
  19. let username = this.login.get(&#39;username&#39;).value;
  20. let password = this.login.get(&#39;password&#39;).value;
  21. if (username == &#39;abc&#39; &amp;&amp; password == &#39;123&#39;) {
  22. this.router.navigate([&#39;/dashboard&#39;]); //navigate when your criteria match then redirect dashboard page otherwise give an error.(Navigation)
  23. }
  24. else {
  25. console.log(&quot;error&quot;);
  26. }
  27. }
  28. Do Not Forget this line in Html page
  29. &lt;form [formGroup]=&quot;login&quot; (ngSubmit)=&quot;onSubmit()&quot;&gt;

huangapple
  • 本文由 发表于 2020年1月3日 15:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定