问题
Here i wrote a small Validation Property for EName Validation when i try 2 Load Html page i'm getting Error as 'ngClass' since it isn't a known property of 'div'. (
Component.ts
import { Component, OnInit } from "@angular/core"
import { Employee } from "../../../templates/employee/employee"
import { Validators, FormGroup, FormBuilder } from "@angular/forms"
@Component({
selector: "customer-ui",
templateUrl: "../../../templates/customer/customer.html"
})
export class JamComponent implements OnInit {
EmpleoyeeForm: FormGroup;
public constructor(private fb: FormBuilder) {}
ngOnInit(): void {
this.EmpleoyeeForm = this.fb.group({
EmpName: ['', [Validators.required]]
})
}
Htmlcode
<form class="form-horizontal" novalidate [formGroup]="EmpleoyeeForm">
<fieldset>
<div class="form-group" [ngClass]="{'has-error': (EmpleoyeeForm.get('EmpName').touched ||
EmpleoyeeForm.get('EmpName').dirty) &&
!EmpleoyeeForm.get('EmpName').valid }">
<label for="name">Name</label>
<input type="text" class="form-control" formControlName="EmpName" [(ngModel)]="EmpName" />
</div>
</fieldset>
</form>
回答1:
You need to add BrowserModule
@NgModule(
imports: [BrowserModule, /* other imports */],
...
)
If it is in a child module import CommonModule.
import { CommonModule } from "@angular/common"
回答2:
If you already have the CommonModule/BrowserModule and it's still not working, also make sure you typed the attribute correctly. I had ngclass
instead of ngClass
(notice the 'C').
回答3:
import { CommonModule } from '@angular/common';
...
imports: [CommonModule],
来源:https://stackoverflow.com/questions/44870542/angular-validation-error-as-ngclass-since-it-isnt-a-known-property-of-div