以编程方式使用 AsyncPipe - Angular 2

Ign*_*asi 3 angular-pipe angular

我正在使用一个组件来像数据一样呈现表格,我给它列的列表、数据、数据如何映射到每列,最后是一个应用于每列数据的管道列表。

到目前为止一切顺利,唯一的问题是当这些管道之一是异步管道时......

经过一段时间的试验,我发现在模板上使用 asyncpipe 时,transform 方法会被多次调用。但是,如果我以编程方式使用它,转换方法只会被调用一次(我调用它的时间)。

我猜它在模板上被多次调用的原因是因为它是一个不纯的管道,但我该如何以编程方式处理它?

这是一个plunker展示了我刚才所说的内容:

@Injectable()
export class AsyncProvider {
  constructor() {}
  
  getById(id: number) {
    // Just some mock data
    return Observable.of({id, times_five: id*5}).delay(1000);
  }
}

@Component({
  selector: 'my-app',
  providers: [AsyncPipe, AsyncProvider]
  template: `
    <div>
      <p>Template async pipe</p>
      <p>{{ asyncObj | async | json }}</p>
      <hr>
      <p>Programmatically async pipe</p>
      <p>{{ asyncObjPiped | json }}</p>
    </div>
  `,
  directives: []
})
export class App {
  constructor(
    private _provider: AsyncProvider,
    private _async: AsyncPipe
  ) {
    this.asyncObj = this._provider.getById(123);
    this.asyncObjPiped = this._async.transform(this.asyncObj);
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 因为AsyncPipe在收到新值时对ChangeDetectorRef执行 markForCheck(),我还尝试了以下操作:

export class App {
  constructor(
    private _provider: AsyncProvider,
    private _async: AsyncPipe,
    private _ref: ChangeDetectorRef,
  ) {
    this.asyncObj = this._provider.getById(123);
    this.asyncObjPiped = this._async.transform(this.asyncObj);
    
    setInterval(() => {
      this._ref.detectChanges();
    }, 1000);
  }
}
Run Code Online (Sandbox Code Playgroud)

没有任何成功:(

Ign*_*asi 5

经过一番挣扎,我设法得到了一些结果,这就是我必须做的,以供将来参考。

第一种方法: plunker

export class App {
  constructor(
    private _provider: AsyncProvider,
    private _async: AsyncPipe,
  ) {
    
    this.asyncObj = this._provider.getById(123)
    
    let processPipe = () => {
      this.asyncObjPiped = this._async.transform(new_asyncObj);
    }
    let new_asyncObj = this.asyncObj.finally(processPipe).repeat(2);
    processPipe();
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,需要一个新变量 (new_asyncObj),因为 finally 似乎返回一个新对象而不是修改现有对象。而在 finally 之后的 repeat(2) 将解开承诺。


方法二: plunker

export class App {
  constructor(
    private _provider: AsyncProvider,
    private _async: AsyncPipe,
  ) {
    
    this.asyncObj = this._provider.getById(123)
    
    setInterval(() => {
      this.asyncObjPiped = this._async.transform(this.asyncObj);
    }, 500);
    
  }
}
Run Code Online (Sandbox Code Playgroud)

每 500 毫秒重新计算一次管道,简单而有效,即使我期待更好的东西。


最终方法: plunker

export class App {
  constructor(
    private _provider: AsyncProvider,
    private _async: AsyncPipe,
    private _ref: ChangeDetectorRef,
    private zone: NgZone,
  ) {
    
    this.asyncObj = this._provider.getById(123)
      
    this.zone.onMicrotaskEmpty
      .subscribe(() => {
        this.asyncObjPiped = this._async.transform(this.asyncObj);
        this.asyncObjPiped = this._async.transform(this.asyncObj);
        this._ref.detectChanges();
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

使用 NgZone 和 ChangeDetectorRef 似乎也能工作,甚至认为一些丑陋的黑客调用 AsyncPipe 两次来解开该值。


无论如何,希望它真的可以帮助那些对以编程方式处理非纯管道感到沮丧的人!

仍然欢迎任何建议或更好的答案!