Angular忽略了离子自定义指令
我在爱奥尼亚创建了一个自定义指令Angular忽略了离子自定义指令,angular,ionic-framework,directive,Angular,Ionic Framework,Directive,我在爱奥尼亚创建了一个自定义指令 ionic g directive directives/time-value 我的代码是 import { Directive, OnInit, Input, HostListener } from '@angular/core'; @Directive({ selector: '[appTimeValue]', }) export class TimeValueDirective implements OnInit{ @Input('a
ionic g directive directives/time-value
我的代码是
import { Directive, OnInit, Input, HostListener } from '@angular/core';
@Directive({
selector: '[appTimeValue]',
})
export class TimeValueDirective implements OnInit{
@Input('appTimeValue') myStyles: any;
constructor() {
console.log("I'm here");
}
ngOnInit(): void {
console.log("I'm here");
}
}
然后我试着使用指令
<ion-input type="text" name="fineColazione" [(ngModel)]="fineColazione" appTimeValue></ion-input>
如何使该指令起作用?TimeValueDirective应该在包含使用该指令的组件的模块中声明。另外,在应用程序内部使用所有可重用组件/指令/管道创建SharedModule,并在任何地方导入此模块也是一种很好的做法。是否直接在AppComponent模板内部使用TimeValueDirective?如果不是,则应在共享模块中导入指令,该模块在任何地方导入CLI在主模块app.module.ts中添加了该指令。我想所有的madules都可以使用。不,它在angular中不是这样工作的。所有声明仅在模块中共享。当您将共享模块导入其中一个模块时,
exports
部分中的模块也是共享的。在我的项目中,我有AppModule,然后还有另外两个模块设置SpageModule和HomePageModule,它们是延迟加载的,但我在CLI生成的项目结构中找不到SharedModule。如果我将该指令包括在SettingsPageModule中,则不会发生任何更改。如果该指令在SettingsPageModule组件中使用,则将其导入其中。也可以像[appTimeValue]
一样使用它。在这种情况下,您将看到NotFound错误,而不是not working指令
@NgModule({
declarations: [
AppComponent,
TimeValueDirective,
],
entryComponents: [],
imports: [
CommonModule,
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
FormsModule
],
providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }],
bootstrap: [AppComponent],
})
export class AppModule {}