gpt4 book ai didi

Angular 5 无法读取未定义的属性 'remove'

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:57 25 4
gpt4 key购买 nike

所以,我一直在努力学习 ASP.NET Core 2 和 Angular 5(我是 Django/Rails Vue 的人),在做了一些简单的例子之后,我决定构建我的一个旧产品新堆栈。问题是我无法找出此错误的来源。

这是我的登录组件模板:

<div class="login-container">
<h2 class="login-title">{{title}}</h2>
<form [formGroup]="form" (ngSubmit)="onSubmit()" class="form-horizontal">
<div class="form-group" *ngClass="{' has-errors has-feedback' : hasErrors('Username')}">
<input type="text" formControlName="Username" class="form-control" required placeholder="Usuario o Email" />
</div>
<div class="form-group" *ngClass="{' has-errors has-feedback' : hasErrors('Password')}">
<input type="password" formControlName="Password" class="form-control" required placeholder="*****" />
</div>
<button type="submit" [disabled]="form.invalid" class="btn btn-md btn-success">Entrar</button>
</form>
</div>

这是 typescript :

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {


title: string;
form: FormGroup;

constructor(private router: Router,
private fb: FormBuilder,
private authService: AuthService,
@Inject('BASE_URL') private baseUrl: string) {
this.title = 'Entrar';
}

ngOnInit(): void {
this.createForm();
}

createForm() {
this.form = this.fb.group({
Username: ['', Validators.required],
Password: ['', Validators.required]
});
console.log(this.form);
}
onBack() {
this.router.navigate(['home']);
}
getFormControl(name: string) {
return this.form.get(name);
}
isValid(name: string) {
const e = this.getFormControl(name);
return e && e.valid;
}
isChanged(name: string) {
const e = this.getFormControl(name);
return e && (e.dirty || e.touched);
}

hasErrors(name) {
return this.isChanged(name) && !this.isValid(name);
}

onSubmit() {
const url = this.baseUrl + 'api/token/auth';
const username = this.form.value.Username;
const password = this.form.value.Password;
this.authService.login(username, password)
.subscribe(res => this.router.navigate(['home']),
err => this.form.setErrors({ 'auth': 'Usuario o password incorrecto' }));
}
}

我在 SO 中读过类似的问题,但最终决定发布这个,因为:

  1. 据我所知,我的模板中没有任何内容是 undefined(我认为这里唯一的绑定(bind)是 form)
  2. 我不会调用/读取其中包含名称 remove 的任何内容。

提前致谢。

最佳答案

我不小心添加了 [ngClass] 时遇到了同样的错误至 <ng-container />

关于Angular 5 无法读取未定义的属性 'remove',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50765359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com