I am getting the following error: ERROR TypeError: Cannot read property 'invalid' of undefined
I am getting the following error: ERROR TypeError: Cannot read property 'invalid' of undefined
我尝试过其他解决方案,例如修复表单引用,但这对我没有帮助。这是我的登录表格
<form [formGroup]="longinForm" (ngSubmit)="onSubmit()">
<h1>Login Form</h1>
<div class="form-group" >
<label for="name" >Username</label>
<input type="text" name ="user" [(ngModel)]="loginuserdata.user"
#user="ngModel"
class="form-control" required >
<div *ngIf="submitted">
<div [hidden]="user.valid || user.pristine" class="alert alert-danger">
Username is required
</div>
</div>
</div>
<div class="form-group" >
<label for="pass" >Password</label>
<input type="text" name="pass" [(ngModel)]="loginuserdata.pass" #pass="ngModel"
class="form-control" required >
<div *ngIf="submitted">
<div [hidden]="pass.valid || pass.pristine" class="alert alert-danger">
Password is required
</div>
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
这也是我的登录组件
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { LoginService } from './login.service';
import { ILoginData } from './login-data';
@Component({
selector: 'app-loginform',
templateUrl: './loginform.component.html',
styleUrls: ['./loginform.component.css']
})
export class LoginformComponent implements OnInit {
loginForm:FormGroup;
loginuserdata : any[] = [];
error:string;
submitted=false;
constructor(private route: Router,private service:LoginService) { }
get f(){return this.loginForm.controls;}
ngOnInit() {
this.onSubmit();
}
onSubmit(){
if(this.loginForm.invalid){return;}//form invalid stop here
//form is valid do something
this.submitted=true;
if(this.f.user.value == "Admin" && this.f.pass.value == "Apassword"){
this.service.getAdmin()
.subscribe(data => this.loginuserdata = data)
this.route.navigateByUrl('admin');
err => this.error = err;
console.log(this.error);
}
}
}
如果你们需要查看我的代码的任何其他部分,请告诉我,请帮助我,我尝试了类似于解决此问题的答案,但它对我不起作用。
你这样做怎么样。
ngAfterViewInit() {
this.onSubmit()
}
我认为它还没有在 ngOninit 中定义。
检查生命周期。
您需要构建表单控件,您已经定义了它,但您需要如下内容。您需要初始化表单组。
import { NgForm, FormGroup, FormBuilder } from '@angular/forms';
//Be sure to import formBuilder and inject through constructor. This will allow you
//to manage how you will build the form and add validation.
//Its a helper class provided by Angular to help us explicitly declare forms.
constructor( private formBuilder: FormBuilder ){}
public ngOnInit(): void {
this.loginForm = this.formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
在这里您可以为表单组定义控件和验证器。 (这个例子只是规定每个都需要一个值才能有效。这应该建立在 ngOnInit
.
然后在您的模板中,您可以删除 [(ngModel)]
并使其如下所示。当您需要访问它时,输入的值将保存在控件中。
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<input type="text" name ="user" [formControlName]="'username'"
class="form-control" required>
<form>
以上将作为登录用户名/电子邮件的示例。
另外如评论中所述,您在 HTML.
的表格中有错字
当您尝试访问 from 组的 valid
和 invalid
属性时,这应该是有效的。有问题可以评论
我尝试过其他解决方案,例如修复表单引用,但这对我没有帮助。这是我的登录表格
<form [formGroup]="longinForm" (ngSubmit)="onSubmit()">
<h1>Login Form</h1>
<div class="form-group" >
<label for="name" >Username</label>
<input type="text" name ="user" [(ngModel)]="loginuserdata.user"
#user="ngModel"
class="form-control" required >
<div *ngIf="submitted">
<div [hidden]="user.valid || user.pristine" class="alert alert-danger">
Username is required
</div>
</div>
</div>
<div class="form-group" >
<label for="pass" >Password</label>
<input type="text" name="pass" [(ngModel)]="loginuserdata.pass" #pass="ngModel"
class="form-control" required >
<div *ngIf="submitted">
<div [hidden]="pass.valid || pass.pristine" class="alert alert-danger">
Password is required
</div>
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
这也是我的登录组件
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { LoginService } from './login.service';
import { ILoginData } from './login-data';
@Component({
selector: 'app-loginform',
templateUrl: './loginform.component.html',
styleUrls: ['./loginform.component.css']
})
export class LoginformComponent implements OnInit {
loginForm:FormGroup;
loginuserdata : any[] = [];
error:string;
submitted=false;
constructor(private route: Router,private service:LoginService) { }
get f(){return this.loginForm.controls;}
ngOnInit() {
this.onSubmit();
}
onSubmit(){
if(this.loginForm.invalid){return;}//form invalid stop here
//form is valid do something
this.submitted=true;
if(this.f.user.value == "Admin" && this.f.pass.value == "Apassword"){
this.service.getAdmin()
.subscribe(data => this.loginuserdata = data)
this.route.navigateByUrl('admin');
err => this.error = err;
console.log(this.error);
}
}
}
如果你们需要查看我的代码的任何其他部分,请告诉我,请帮助我,我尝试了类似于解决此问题的答案,但它对我不起作用。
你这样做怎么样。
ngAfterViewInit() {
this.onSubmit()
}
我认为它还没有在 ngOninit 中定义。 检查生命周期。
您需要构建表单控件,您已经定义了它,但您需要如下内容。您需要初始化表单组。
import { NgForm, FormGroup, FormBuilder } from '@angular/forms';
//Be sure to import formBuilder and inject through constructor. This will allow you
//to manage how you will build the form and add validation.
//Its a helper class provided by Angular to help us explicitly declare forms.
constructor( private formBuilder: FormBuilder ){}
public ngOnInit(): void {
this.loginForm = this.formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
在这里您可以为表单组定义控件和验证器。 (这个例子只是规定每个都需要一个值才能有效。这应该建立在 ngOnInit
.
然后在您的模板中,您可以删除 [(ngModel)]
并使其如下所示。当您需要访问它时,输入的值将保存在控件中。
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<input type="text" name ="user" [formControlName]="'username'"
class="form-control" required>
<form>
以上将作为登录用户名/电子邮件的示例。 另外如评论中所述,您在 HTML.
的表格中有错字当您尝试访问 from 组的 valid
和 invalid
属性时,这应该是有效的。有问题可以评论