typescript myngclass-6.ts

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript myngclass-6.ts相关的知识,希望对你有一定的参考价值。

@Directive({
  selector: '[myNgClass]'
})
export class MyNgClassDirective {
  @Input() myNgClass = {};
  private differ: KeyValueDiffer<any, any>;

  constructor(private differs: KeyValueDiffers, private host: ElementRef, private renderer: Renderer2) {
    this.differ = this.differs.find(this.myNgClass).create();
  }

  ngOnInit() {
    ...add the initial classes
  }

  ngDoCheck() {

    const changes = this.differ.diff(this.myNgClass);

    if (changes) {
      changes.forEachAddedItem((record: KeyValueChangeRecord<any, any>) => {
        if (record.currentValue === true) {
          this.renderer.addClass(this.host.nativeElement, record.key);
        }
      });

      changes.forEachChangedItem((record: KeyValueChangeRecord<any, any>) => {
        if (record.currentValue === true) {
          this.renderer.addClass(this.host.nativeElement, record.key);
        } else {
          this.renderer.removeClass(this.host.nativeElement, record.key);
        }
      });

      changes.forEachRemovedItem((record: KeyValueChangeRecord<any, any>) => {
        this.renderer.removeClass(this.host.nativeElement, record.key);
      });
    }

  }

}

以上是关于typescript myngclass-6.ts的主要内容,如果未能解决你的问题,请参考以下文章

typescript TypeScript Snippets #typescript

TypeScript入门五:TypeScript的接口

TypeScript系列教程--初探TypeScript

TypeScript入门三:TypeScript函数类型

typescript使用 TypeScript 开发 Vue 组件

认识 TypeScript