Angular 角度4异常:Can';t绑定到';ngClass';因为它不是';t'的已知属性;输入';
在我的项目中,我使用延迟加载,因此,在我的注册模块中,我使用Angular 角度4异常:Can';t绑定到';ngClass';因为它不是';t'的已知属性;输入';,angular,typescript,Angular,Typescript,在我的项目中,我使用延迟加载,因此,在我的注册模块中,我使用[ngClass]指令在formGroup注册表单上出现一些验证错误时添加无效的类。但是我的代码在尝试在表单上添加[ngClass]指令时引发异常 无法绑定到“ngClass”,因为它不是“input”的已知属性 在调查错误本身时,我找到了几种解决方案,比如在组件中添加“directive:[NgStyle]”,但这并不能解决问题 这是我的密码: 注册.路由器.ts import { NgModule } from '@angular/
[ngClass]
指令在formGroup
注册表单上出现一些验证错误时添加无效的类。但是我的代码在尝试在表单上添加[ngClass]
指令时引发异常
无法绑定到“ngClass”,因为它不是“input”的已知属性
在调查错误本身时,我找到了几种解决方案,比如在组件中添加“directive:[NgStyle]”,但这并不能解决问题
这是我的密码:
注册.路由器.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RegisterComponent } from "app/modules/register/register.component";
const routes: Routes = [
{
path: '', pathMatch: 'full',
component: RegisterComponent
}
];
@NgModule({
imports: [
RouterModule.forChild(routes),
FormsModule,
ReactiveFormsModule
],
declarations: [RegisterComponent],
exports: [RouterModule]
})
export class RegisterRouter { }
寄存器模块.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RegisterRouter } from './register.router';
@NgModule({
imports: [
CommonModule,
RegisterRouter
],
declarations: []
})
export class RegisterModule { }
import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
//#region Declarations
UserForm: FormGroup;
inValid: boolean = false;
//#endregion
constructor(
private _fb: FormBuilder) {
this.UserForm = _fb.group({
"_firstname" : ['', Validators.required]
});
}
}
register.component.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RegisterRouter } from './register.router';
@NgModule({
imports: [
CommonModule,
RegisterRouter
],
declarations: []
})
export class RegisterModule { }
import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
//#region Declarations
UserForm: FormGroup;
inValid: boolean = false;
//#endregion
constructor(
private _fb: FormBuilder) {
this.UserForm = _fb.group({
"_firstname" : ['', Validators.required]
});
}
}
register.component.html
<input type="text" class="form-control" [ngClass]="{'ahinValid': inValid}" id="txtFirst_Name" aria-describedby="ariaFirstName" placeholder="Enter First Name"
name="_firstname" [formControl]="UserForm.controls['_firstname']">
谢谢您的帮助。由于
注册表组件
是在RegisterRouter
(模块的名称是什么?)模块中声明的,因此您必须导入CommonModule
:
@NgModule({
imports: [
RouterModule.forChild(routes),
FormsModule,
ReactiveFormsModule,
CommonModule <================== this line
],
declarations: [
RegisterComponent // this component wants to have access to built-in directives
],
exports: [RouterModule]
})
export class RegisterRouter { }
@NgModule({
进口:[
用于儿童的路由模块(路由),
FormsModule,
反应形式模块,
CommonModule延迟加载共享模块问题通用解决方案:
两个模块都必须导入CommonModule
在共享模块中:
@NgModule({
imports: [CommonModule],
...
})
要在其中使用组件的模块:
@NgModule({
imports: [CommonModule, ...],
...
})
对于导入CommonModule
我有一个无法编译的库。我有几个模块。它们正在导入其他较小的/组件:
我的一个较小模块中的一个组件中有[ngClass],这会导致lib无法编译,并且抛出无法绑定到'ngClass',因为它不是'div'的已知属性。
显然…我正在导出该模块的所有组件…但不是public api.ts
文件中的模块本身(导入CommonModule的模块):
export*from./lib/af layout/af layout.module';//您需要导入commonModule
@NgModule({
imports: [CommonModule],
...
})
除了父模块(例如应用程序模块)的导入中包含的通用模块之外,还要确保引发问题的组件实际包含在父模块的声明中。添加通用模块对我也不起作用。
将我的组件添加到其模块的“声明”中非常有效
Reference-延迟加载与您的表单无关。因此,您可以将其丢弃,就像您的错误源一样。这是在编译之前、期间还是之后?@zron after compilement在您的主应用程序模块中,您是否导入BrowserModule
?您是否尝试过:[ngClass]=“{ahinValid':inValid}”
(带内引号)?忘记在这里导入CommonModule
。不,这不是我要的,我已经导入了CommonModuleeverywhere@gyozokudor你能在某处复制它吗?你有任何github存储库我可以看一下吗?这是在我移动了一些文件之后发生的。我必须重新启动ng serve,错误已经消除。我需要重新启动所有东西,e.g.Thread start,Thread start:API等一起运行了两次…我花了很多时间试图解决这个问题。我认为编译器错误对于库编译应该更好…Tx。你刚刚为我的库组件节省了大约半天的试用和错误修复谢谢!谢谢!你救了我几天的命.