Angular 5无法读取未定义的属性“删除”

yor*_*odm 1 typescript angular angular5

因此,我一直在努力进入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>
Run Code Online (Sandbox Code Playgroud)

这是打字稿:

@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' }));
  }
}
Run Code Online (Sandbox Code Playgroud)

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

  1. 据我所知,我的模板中没有任何内容undefined(我认为这里唯一的绑定是form
  2. 我没有打电话/什么也没有读名字remove

提前致谢。

yor*_*odm 8

通过模板代码找到答案:

代替*ngClass它应该是[ngClass]因为*结构指令的简写。编写* ngClass对进行了一些错误的调用DOM,这就是为什么我最终得到该错误的原因。


nic*_*ick 5

意外添加[ngClass]到相同的错误<ng-container />