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

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

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

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

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 (

[ERROR ->]

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.

&lt;div [formGroup]=&quot;myGroup&quot;&gt;
  &lt;input formControlName=&quot;firstName&quot;&gt;
&lt;/div&gt;

In your class:

this.myGroup = new FormGroup({
   firstName: new FormControl()
});

login.component.html

 &lt;div class=&quot;card-body&quot;&gt;
        &lt;form [formGroup]=&quot;logincreteform&quot; (ngSubmit)=&quot;onSubmit()&quot;&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                 &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;
              &lt;/div&gt;

             &lt;div class=&quot;form-group&quot;&gt;
                   &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;
             &lt;/div&gt;
            
             &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary px-4&quot;&gt;Login&lt;/button&gt;
      &lt;/form&gt;
&lt;/div&gt;

login.component.ts

import { Component, OnInit,OnChanges, Input } from &#39;@angular/core&#39;;
import { Router } from &#39;@angular/router&#39;;
import { FormGroup, FormsModule, FormControl, ReactiveFormsModule, Validators} from &quot;@angular/forms&quot;;

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 == &#39;abc&#39; &amp;&amp; password == &#39;123&#39;) {
            this.router.navigate([&#39;/register&#39;]);
        }
        else {
            console.log(&quot;error&quot;);
        }
    }
}

app.module.ts

import { FormsModule,ReactiveFormsModule } from &#39;@angular/forms&#39;; 
import { LoginComponent } from &#39;./views/login/login.component&#39;;
import { RegisterComponent } from &#39;./views/register/register.component&#39;;

@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 (&quot;
          &lt;div class=&quot;card mx-4&quot;&gt;
            &lt;div class=&quot;card-body p-4&quot;&gt;
              [ERROR -&gt;]&lt;form&gt;
                &lt;h1&gt;Register&lt;/h1&gt;
                &lt;p class=&quot;text-muted&quot;&gt;Create your account&lt;&quot;): ng:///AppModule/RegisterComponent.html@7:14

register.component.html

          &lt;div class=&quot;card-body p-4&quot;&gt;
              &lt;form&gt;
                &lt;h1&gt;Register&lt;/h1&gt;
                &lt;p class=&quot;text-muted&quot;&gt;Create your account&lt;/p&gt;
                &lt;div class=&quot;form-group&quot;&gt;
                        &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;
                &lt;/div&gt;
               &lt;div class=&quot;form-group&quot;&gt;
                         &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;
               &lt;/div&gt;
               &lt;div class=&quot;form-group&quot;&gt;
                     &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;
               &lt;/div&gt;
                   &lt;button type=&quot;button&quot; class=&quot;btn btn-block btn-success&quot;&gt;Create Account&lt;/button&gt;
         &lt;/div&gt;

register.component.ts

import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-dashboard&#39;,
  templateUrl: &#39;register.component.html&#39;
})
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

&lt;div class=&quot;card-body p-4&quot;&gt;
 &lt;form [formGroup]=&quot;registerform&quot; (ngSubmit)=&quot;onSubmit()&gt;
   &lt;h1&gt;Register&lt;/h1&gt;
   &lt;p class=&quot;text-muted&quot;&gt;Create your account&lt;/p&gt;
   &lt;div class=&quot;form-group&quot;&gt;
      &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;
                &lt;/div&gt;
               &lt;div class=&quot;form-group&quot;&gt;
                         &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;
               &lt;/div&gt;
               &lt;div class=&quot;form-group&quot;&gt;
                     &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;
               &lt;/div&gt;
                   &lt;button type=&quot;button&quot; class=&quot;btn btn-block btn-success&quot;&gt;Create Account&lt;/button&gt;
         &lt;/div&gt;

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

`import { Component } from &#39;@angular/core&#39;;
import { FormsModule,ReactiveFormsModule } from &#39;@angular/forms&#39;; 
@Component({
  selector: &#39;app-dashboard&#39;,
  templateUrl: &#39;register.component.html&#39;
})
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 &#39;@angular/core&#39;;
import { Router } from &#39;@angular/router&#39;;
import { FormGroup, FormControl, Validators, FormBuilder} from &quot;@angular/forms&quot;;

@Component({
    selector: &#39;app-dashboard&#39;,
    templateUrl: &#39;login.component.html&#39;
})

export class LoginComponent  {

    login: FormGroup;

    constructor(private frmbuilder: FormBuilder, public router:Router) {

        this.login = this.frmbuilder.group({
            username: [&#39;&#39;],
            password: [&#39;&#39;]

        });
    }

 onSubmit() {
        console.log(this.login);
        let username = this.login.get(&#39;username&#39;).value;
        let password = this.login.get(&#39;password&#39;).value;
        if (username == &#39;abc&#39; &amp;&amp; password == &#39;123&#39;) {
            this.router.navigate([&#39;/dashboard&#39;]);     //navigate when your criteria match then redirect dashboard page otherwise give an error.(Navigation)
        }
        else {
            console.log(&quot;error&quot;);
        }
    }

Do Not Forget this line in Html page

 &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:

确定