2

为什么自定义管道的变换方法在这个 angular2 循环中使用时会触发多次?我预计它只会发射两次。

非常感谢!

结果在浏览器的控制台中

app.component.ts

import {Component} from 'angular2/core';
import {MyPipe} from './my.pipe';

@Component({
  selector: 'my-app',
  pipes: [MyPipe],
  template: `
    <div *ngFor="let country of countries">
      {{ country | myPipe }}
    </div>
  `
})

export class AppComponent {

  public countries: any[] = [];

  constructor() {
    this.countries.push('Spain');
    this.countries.push('Italy');
  }
}

我的.pipe.ts

import {Pipe} from 'angular2/core';

@Pipe({
  name: 'myPipe'
})

export class MyPipe {
  constructor() {
    console.log('MyPipe');
  }

  transform(param: any) {
    console.log(param);
    return param;
  }
}

main.ts

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';

bootstrap(AppComponent);
4

0 回答 0