所以,我一直在努力进入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>
这是打字稿:
@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中读过类似的问题但最终决定发布这个问题是因为:
undefined
(我认为此处唯一的约束是form
)remove
。提前致谢。
答案 0 :(得分:3)
完成模板代码后找到答案:
而不是*ngClass
它应该是[ngClass]
,因为*
是结构指令的简写。写* ngClass会对DOM
进行一些错误的调用,这就是我最终得到错误的原因。
答案 1 :(得分:3)
将[ngClass]
意外添加到<ng-container />
时出现了相同的错误