如何在角度5中创建启用了“ as语法”的自定义指令?

时间:2018-08-31 13:44:28

标签: angular angular-directive

在角度5中,ngIf指令-as syntax内置了功能。您可以在https://toddmotto.com/angular-ngif-async-pipe处找到示例。

在下面的示例中,user$是可见的。

<div *ngIf="(user$ | async) as user; else loading">
  <user-profile
    [user]="user.profile">
  </user-profile>
  <user-messages
    [user]="user.messages">
  </user-messages>
</div>

<ng-template #loading>
  Loading stuff...
</ng-template>

我创建了自己的指令,该指令将元素的内容替换为加载微调器,并且我希望启用将observable传递为输入,请参见下面的代码。

@Directive({ selector: '[appLoadingData]' })
export class LoadingDataDirective {
  constructor(
    private templateRef: TemplateRef<any>,
    private viewContainerRef: ViewContainerRef,
    private componentFactoryResolver: ComponentFactoryResolver
  ) { }
  @Input()
  set appLoadingData(data: any) {
    if (data != null) {
      if (data.subscribe) {
        this.addLoadingPlaceholder();
        data.subscribe(() => {
          setTimeout(() => this.addView(), 0);
        });
      } else {
        this.addView();
      }
    } else {
      this.addLoadingPlaceholder();
    }
  }

  private addView() {
    this.viewContainerRef.clear();
    this.viewContainerRef.createEmbeddedView(this.templateRef);
  }

  private addLoadingPlaceholder() {
    this.viewContainerRef.clear();
    const loadingComponentFactory = this.componentFactoryResolver.resolveComponentFactory(LoadingDataComponent);
    this.viewContainerRef.createComponent(loadingComponentFactory);
  }
}

我想以另一种方式使用

    <ng-container *ngFor="let chartConfig of chartsConfigs">
      <div class="chart-wrapper"
        *appLoadingData="(chartConfig.chartGroups$ | async) as groups">
        <chart-summary [chartData]="groups"
          [chartOptionsName]="chartConfig.chartType">
        </chart-summary>
      </div>
    </ng-container>

或者最好是*appLoadingData="chartConfig.chartGroups$ as groups"

没有语法(*appLoadingData="chartConfig.chartGroups$ | async"[chartData]="chartConfig.chartGroups$ | async"),在我的加载程序被解除后,存在延迟,我认为这是因为在指令的addView()上,异步管道被第二次调用了。我想像ngIf指令一样实现as syntax。我尝试这样使用它:*appLoadingData="(chartConfig.chartGroups$ | async) as groups",但是不幸的是,在这种情况下,groups始终是undefined

如何在自定义指令中实现as syntax

1 个答案:

答案 0 :(得分:0)

您可以看到他们正在使用_context变量。 https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts

也请参阅@yurzui答案。 How to declare a variable in a template in Angular2

我的最终结果:

export class LoadingDataDirective {
  context: any = {};
  constructor(
    private templateRef: TemplateRef<any>,
    private viewContainerRef: ViewContainerRef,
    private componentFactoryResolver: ComponentFactoryResolver
  ) { }
  @Input()
  set appLoadingData(data: any) {
    if (data != null) {
      if (this.isObservable(data)) {
        this.addLoadingPlaceholder();
        const subscription = data.subscribe((observableResult: any) => {
          this.addView(observableResult);
          subscription.unsubscribe();
        });
      } else {
        this.addView(data);
      }
    } else {
      this.addLoadingPlaceholder();
    }
  }

  private addView(contextData: any) {
    this.context.$implicit = this.context.appLoadingData = contextData;
    this.viewContainerRef.clear();
    this.viewContainerRef.createEmbeddedView(this.templateRef, this.context);
  }

  private addLoadingPlaceholder() {
    this.viewContainerRef.clear();
    const loadingComponentFactory = this.componentFactoryResolver.resolveComponentFactory(LoadingDataComponent);
    this.viewContainerRef.createComponent(loadingComponentFactory);
  }

  private isObservable(data: any): boolean {
    return !!data.subscribe;
  }
}

chartConfig.charGroup$-是可观察的

   <ng-container *ngFor="let chartConfig of chartsConfigs">
      <div class="chart-wrapper"
        *appLoadingData="chartConfig.chartGroups$ as groups">
        <chart-summary [chartData]="groups"
          [chartOptionsName]="chartConfig.chartType">
        </chart-summary>
      </div>
    </ng-container>