Angular 6 numberonly指令不起作用

Nil*_*ara 3 angular-directive angular angular6

我已经从https://www.davebennett.tech/angular-4-input-numbers-directive/创建了一条指令,以便可以限制用户仅在电话号码中输入数字。在src/app/app.sharerd.module.ts文件中,我做了以下代码来导入指令:

import { NumberOnlyDirective } from './number.directive';
declarations: [..., ..., NumberOnlyDirective], ...
export class SharedModule { }
Run Code Online (Sandbox Code Playgroud)

现在,在/src/文件夹下,我创建了名为的文件夹modules/auth/component/

auth.module.ts/src/auth/文件夹下,我已执行以下操作:

import { NgModule } from '@angular/core';
import { SharedModule } from '../../app/app.shared.module';
...
...
Run Code Online (Sandbox Code Playgroud)

现在,在signup.html下面/src/auth/component/

<input type="text" name="phone" myNumberOnly ... > ...
...
Run Code Online (Sandbox Code Playgroud)

我仍然可以在文本框中输入字符/特殊字符等,但是在console / cli中没有看到任何错误。

JSt*_*Stw 6

在共享模块中使用自定义指令/管道时,还需要将其导出。

基本上在您的教程中,他创建了指令并在app模块中对其进行了声明。但是在您的示例中,您将指令放置在共享模块中,因此您需要将指令放置在声明括号中,也需要放置在导出文件中。

shared.module.ts

@NgModule({
    /* ... */
    declarations: [YourDirective],
    exports: [YourDirective]
    /* ... */
})
Run Code Online (Sandbox Code Playgroud)