为什么自定义管道的转换方法在Angular2中多次触发?

时间:2016-08-12 19:51:46

标签: angularjs angular ngfor angular-pipe

为什么自定义管道的转换方法会像在此angular2环路中使用时那样多次触发?我希望它只发射两次。

非常感谢!

Result in the browser's console

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');
  }
}

my.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);

0 个答案:

没有答案
相关问题