Testing directive in Angular 2 + Jasmine I am gett

2019-08-21 07:04发布

I am getting the error below when I run the test using Angular 2 + Jasmine. Does anyone know how can I solve this problem?

I already have imported the directive in app.module.ts.

Can't bind to 'highlightData' since it isn't a known property of 'li'. ("[ERROR ->][highlightData]="item.name">

list.component.ts

<ul>
    <li *ngFor="let item of list" [highlightData]="item.name">{{item.name}}</li>
</ul>

highlight-data.directive.ts

import { Directive, SimpleChanges, Input, OnChanges, ElementRef, Renderer} from '@angular/core';

@Directive({
  selector: '[highlightData]'
})
export class HighlightDataDirective implements OnChanges {
  private _highlightData: string;

  @Input() set highlightData(value: string) {
    const prev = this._highlightData;
    this._highlightData = value;
    const cur = value;
  }

  constructor(private _elementRef: ElementRef, private _render: Renderer) {

  }

  ngOnChanges(changes: SimpleChanges) {
    if (changes['highlightData'] && !changes['highlightData'].isFirstChange()) {
      const prev: string = changes['highlightData'].previousValue;
      const cur: string = changes['highlightData'].currentValue;

      if (cur !== prev) {
        this._render.setElementClass(this._elementRef.nativeElement, 'animate', true);

        setTimeout(() => {
          this._render.setElementClass(this._elementRef.nativeElement, 'animate', false);
        }, 3000);
      }
    }
  }

}

Thanks.

0条回答
登录 后发表回答