根据属性值使用类

Jamshaid塔里克

[ngClass] 当我尝试根据class的属性选择Css class时无法正常工作。

[ngClass]="{'alert alert-danger': employeeForm.get('fullname').errors && (employeeForm.get('fullname').touched || employeeForm.get('fullname').dirty) }"

上面的代码可在中使用,*ngIf但不能在中使用[ngClass]

我正在关注YouTube教程

export class CreateEmployeeComponent implements OnInit {
  employeeForm: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.employeeForm = this.fb.group({
      fullname: ['', Validators.required ],
      email: [''],

      skills : this.fb.group({
        skillName: [''],
        experience: [''],
        proficiency: ['beginner']
      })
    });
  }

  onLoadDataClick(): void {
    this.employeeForm.setValue({
      fullname: 'Jamshaid Tariq',
      email: '[email protected]',
      skills: {
        skillName: 'C#',
        experience: 1,
        proficiency: 'beginner'
      }
    });
  }

  onSubmit(): void {
    console.log(this.employeeForm.value);
  }

}
马塞尔·霍斯特拉(Marcel Hoekstra)

尝试通过添加!!来确保它是布尔值:

[ngClass]="{'alert alert-danger': !!(employeeForm.get('fullname').errors && (employeeForm.get('fullname').touched || employeeForm.get('fullname').dirty)) }"

我拿了你的代码,这有效。我只加了一个!测试https://stackblitz.com/edit/angular-fquu8t

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章