首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular11忽略的指令

Angular11忽略的指令
EN

Stack Overflow用户
提问于 2021-01-05 20:02:51
回答 1查看 47关注 0票数 0

我试图为numbers创建一个与reactive form兼容的组件。我的组件包含一个用于格式化数字数据的指令。

代码语言:javascript
复制
<form [formGroup]="parentForm">
    <div class="field">
        <span class="field-label" [innerHtml]="label"></span>
        <div class="input-group">
            <ng-content></ng-content>
            <input class="k-textbox" currencyTransform [formControlName]="fieldName" />
        </div>
    </div>
</form>

currencyTransorm是我的指令,它是这样声明的:

代码语言:javascript
复制
@Directive({
    selector: "[currencyTransform]"
})
export class CurrencyTransformDirective implements OnDestroy {
    ...
}

现在我有了一些不同的模块。一个用于导出所有组件的模块(在本例中仅为NumeriFieldComponent):

代码语言:javascript
复制
@NgModule({
    imports: [
        CommonModule,
        ReactiveFormsModule
    ],
    declarations: [
        NumericFieldComponent
    ],
    exports: [
        NumericFieldComponent
    ]
})
export class ComponentsModule { }

我的测试页面有一个模块:

代码语言:javascript
复制
let routes: Routes = [
    { path: "", component: TestComponent }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class TestRoutingModule { }

@NgModule({
    imports: [
        CommonModule,
        ComponentsModule,
        ReactiveFormsModule,
        TestRoutingModule,
    ],
    declarations: [
        TestComponent
    ]
})
export class TestModule { }

最后一个模块是我在其中声明CurrencyTransformDirectiveapp.module.ts

代码语言:javascript
复制
@NgModule({
    declarations: [
        AppComponent,
        CurrencyTransformDirective
    ],
    imports: [
        // SOME IMPORTS
        AppRoutingModule,
        BrowserAnimationsModule,
        BrowserModule,
        CommonModule
    ],
    providers: [
        // SOME PROVIDERS
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

看起来一切正常,但是我的指令被忽略了。我尝试在构造函数中使用console.log,但没有任何内容写入浏览器控制台。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-01-05 22:21:25

一种可能的解决方案是为指令创建一个新模块。我在ComponentsModule上导入了新模块,现在它工作得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65578505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档