我有SharedModule
import { NgModule } from '@angular/core';
import { ControlMessagesComponent } from './control-messages.component';
@NgModule({
imports: [
],
declarations: [
ControlMessagesComponent
],
exports: [
ControlMessagesComponent,
]
})
export class SharedModule {}
然后我导入了2个不同的模块:
import { SharedModule } from './../shared/shared.module';
@NgModule({
imports: [
SharedModule
],
declarations: [
],
providers: [
]
})
export class OnboardModule {}
import { SharedModule } from '../shared/shared.module';
@NgModule({
imports: [
SharedModule
],
declarations: [
],
providers: [
]
})
export class AModule {}
以下是ControlMessagesComponent
import { Component, Input } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { ValidationService } from './../validators/validator';
@Component({
selector: 'control-messages',
template: `<div *ngIf="errorMessage !== null">{{errorMessage}}</div>`
})
export class ControlMessagesComponent {
@Input() control: FormControl;
constructor() {}
get errorMessage() {
for (const propertyName in this.control.errors) {
if (this.control.errors.hasOwnProperty(propertyName) && this.control.touched) {
return ValidationService.getValidatorErrorMessage(propertyName, this.control.errors[propertyName]);
}
}
return null;
}
}
当我使用它时:
<control-messages [control]="form.controls.assetName"></control-messages>
我收到以下错误:
无法绑定到'ngIf',因为它不是'div'的已知属性。 (“] * ngIf =”errorMessage!== 空 “&GT; {{的errorMessage}}”): NG:///SharedModule/ControlMessagesComponent.html@0:5 属性绑定ngIf未被嵌入式模板上的任何指令使用。确保属性名称拼写正确并且 所有指令都列在“@ NgModule.declarations”中。 (“[错误 - &gt;] {{errorMessage}}“):ng:///SharedModule/ControlMessagesComponent.html@0:0
我没有选择,有人可以让我知道我在这里做错了吗?
答案 0 :(得分:5)
将CommonModule
添加到SharedModule
。 NgIf
和NgForOf
也是CommonModule
中的指令。实际位于SharedModule
的您的组件使用NgIf
,因此您必须将其导入模块中。
包含所有基本Angular指令的模块,如
NgIf
,NgForOf
,
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ControlMessagesComponent } from './control-messages.component';
@NgModule({
imports: [
CommonModule
],
declarations: [
ControlMessagesComponent
],
exports: [
ControlMessagesComponent,
]
})
export class SharedModule {}