无法识别自定义指令

安德烈斯泰希

我创建了一个名为holdable 的自定义指令。

import { Directive, EventEmitter, Output, HostListener,  } from '@angular/core';

import { Observable, Subject, interval } from 'rxjs';
import { takeUntil, tap, filter } from 'rxjs/operators';

@Directive({
  selector: '[appHoldable]'
})
export class HoldableDirective {

  @Output() holdTime: EventEmitter<number> = new EventEmitter();

  state: Subject<string> = new Subject();

  cancel: Observable<string>;

  constructor() { 
    this.cancel = this.state.pipe(
      filter(v => v === 'cancel'),
      tap(v => {
        console.log('Stopped holding.');
        this.holdTime.emit(0);
      }
      )
    );
  }

  @HostListener('mouseup', ['$event'])
  @HostListener('mouseleave', ['$event'])
  onExit(){
    this.state.next('cancel');
  }

  @HostListener('mousedown', ['$event'])
  onHold(){
    console.log('Started holding.');
    this.state.next('start');
    const n = 100;
    interval(n).pipe(
      takeUntil(this.cancel),
      tap(v => {
        this.holdTime.emit(v * n);
      })
    )
    .subscribe()
  }
}

它注册在 AppModule 中,因为我想在整体应用程序中使用它。例如在我的名为 CashRegisterModule 的模块中:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { CashRegisterRoutingModule } from './cash-register-routing.module';

import { CashRegisterComponent } from './cash-register.component';
import { UIModule } from '../UI/ui.module';
import { ScannerComponent } from './steps/scanner/scanner.component';
import { ArticlePreviewComponent } from './steps/article-preview/article-preview.component';
import { OverviewComponent } from './steps/overview/overview.component';
import { PaymentComponent } from './steps/payment/payment.component';

@NgModule({
  declarations: [
    CashRegisterComponent, 
    ScannerComponent, 
    ArticlePreviewComponent, OverviewComponent, PaymentComponent
  ],
  imports: [
    CommonModule,
    CashRegisterRoutingModule,
    UIModule
  ]
})
export class CashRegisterModule { }

而且这个模块也在 app.module.ts 文件中注册 看起来像这样

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { DashboardModule } from './views/dashboard/dashboard.module';
import { UIModule } from './UI/ui.module';
import { ArticleModule } from './views/article/article.module';
import { ArticleService } from './services/article.service';
import { UiService } from './services/ui.service';
import { CashRegisterModule } from './cash-register/cash-register.module';
import { DataService } from './services/data.service';
import { HoldableDirective } from './directives/holdable.directive';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    HoldableDirective,
  ],
  imports: [
    CommonModule,
    BrowserModule,
    HttpClientModule,
    RouterModule,
    ReactiveFormsModule,
    DashboardModule,
    UIModule,
    ArticleModule,
    CashRegisterModule,
    AppRoutingModule
  ],
  providers: [
    ArticleService,
    UiService,
    DataService
  ],
  bootstrap: [
    AppComponent
  ],

})
export class AppModule { }

我在 CashRegisterComponent 的 PaymentComponent 内的按钮中使用指令

<button *ngFor="let moneyType of state.moneyToRender"
      appHoldable (holdTime)="calculateGivenMoney(moneyType, $event)"
      >{{moneyType.toFixed(2)}} €
    </button>

calculateGivenMoney(...) 将像这样初始化

calculateGivenMoney(amount: number, /*operation: string,*/ holdTime: number): number {
    console.log('ich werde geklickt.' + holdTime)
    if(holdTime > 1000){
      this.state.givenMoney = this.state.givenMoney - amount;
    } else {
      this.state.givenMoney = this.state.givenMoney + amount;
    } return this.state.givenMoney;
}

但是两个打印语句都不起作用……

有谁知道什么在我的代码中不起作用?

非常感谢您的帮助,祝您有美好的一天!

ashish.gd

有三个步骤可以使这个工作。它适用于几乎所有 Angular 元素,如组件、指令、管道等。

  1. 在特定模块中声明指令,例如: SharedModule
  2. 从该模块导出指令,即 SharedModule
  3. 在要使用它的组件的父模块中导入该模块。例如:SomeOtherModule

示例代码:

@NgModule({
    declarations: [HoldableDirective],
    exports: [HoldableDirective]
})
export class SharedModule {}


@NgModule({
    declarations: [
        SomeChildComponent
    ],
    imports: [
        ... other modules,
        SharedModule
    ]
})
export class SomeOtherModule

这样,带有相应选择器的指令将对SomeOtherModule.

注意:如果您希望其他模块可以访问模块成员,那么导出模块成员很重要。将其视为模块的公共 API。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails自定义验证无法识别

来自分类Dev

自定义功能无法识别

来自分类Dev

Rails的自定义验证无法识别

来自分类Dev

自定义指令

来自分类Dev

无法使用ngShow设置自定义指令的动画

来自分类Dev

使用自定义指令时,ngShow无法正常工作

来自分类Dev

自定义指令无法通过SSL Angular解决

来自分类Dev

AngularJS:包含无法通过自定义指令运行

来自分类Dev

自定义禁用指令无法正常工作

来自分类Dev

自定义CEditor Eclipse CDT无法识别

来自分类Dev

CakePHP控制器自定义功能无法识别

来自分类Dev

Django自定义命令错误:无法识别的参数

来自分类Dev

组织HTML导出无法识别自定义乳胶宏

来自分类Dev

Android Studio无法识别自定义视图类

来自分类Dev

无法识别JQuery自定义插件功能

来自分类Dev

WPF无法识别自定义类库

来自分类Dev

在自定义单元格中无法识别属性

来自分类Dev

自定义类上的Flash功能无法识别

来自分类Dev

Android Studio无法识别自定义视图类

来自分类Dev

无法识别流星自定义HTML标签

来自分类Dev

自定义ConfigurationSection中无法识别的元素异常

来自分类Dev

自定义CEditor Eclipse CDT无法识别

来自分类Dev

在自定义单元格中无法识别属性

来自分类Dev

在自定义页面模板中无法识别“记录”对象

来自分类Dev

Wear OS 无法识别自定义颜色

来自分类Dev

编译自定义指令

来自分类Dev

自定义搜索指令

来自分类Dev

AngularJS自定义指令

来自分类Dev

指令自定义控件