AngularJs 1.x 中使用来帮助我们转换templates中的输出,但在Angular2中使用的是,以下展示Angular 1.x and Angular 2中filter和pipe的对比:
Angular 1.x | Angular 2 |
---|---|
<span style="color: #008000">//
<span style="color: #008000"><span style="color: #000000">
import {Component,View,bootstrap} from 'angular2/angular2'<span style="color: #000000">;
@Component({
selector: 'pipes'<span style="color: #000000">
})
@View({
templateUrl: 'pipesTemplate.html'<span style="color: #000000">
})
<span style="color: #008000">//<span style="color: #008000"> Component controller
<span style="color: #000000">class PipesAppComponent {
date: Date;
constructor() {
<span style="color: #0000ff">this.date = <span style="color: #0000ff">new<span style="color: #000000"> Date();
}
}
bootstrap(PipesAppComponent);
Dates
{{date | date:'mediumDate'}}
{{date | date:'yMMMMd'}}
{{date | date:'shortTime'}}
结果:
New Pipes
decimal和percent是Angular2中新增的管道,参数规则是:{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
.
decimal管道在模板中使用number关键字
templateUrl: 'pipesTemplate.html'<span style="color: #000000">
})
class PipesAppComponent {
grade: number;
rating: number;
constructor() {
<span style="color: #0000ff">this.rating = 9.1243<span style="color: #000000">;
}
}
...
html
Decimals/Percentages
{{grade | percent:'.2'}}
{{rating | number:'2.1-2'}}
结果:
Async Pipe
Angular 2's async
allows us to bind our templates directly to values that arrive asynchronously. This ability is great for working with promises and observables.
selector: 'pipes'<span style="color: #000000">,changeDetection: 'ON_PUSH'<span style="color: #000000">
})
@View({
templateUrl: 'pipesTemplate.html'<span style="color: #000000">,})
class PipesAppComponent {
promise: Promise;
constructor() {
<span style="color: #0000ff">this.promise = <span style="color: #0000ff">new Promise(<span style="color: #0000ff">function<span style="color: #000000">(resolve,reject) {
setTimeout(<span style="color: #0000ff">function<span style="color: #000000">() {
resolve("Hey,I'm from a promise."<span style="color: #000000">);
},2000<span style="color: #000000">)
});
}
}
...
html
Async
{{ promise | async}}
After a 2 second delay,the value from the resolved promise will be displayed on the screen.
Custom Pipes
自定义pipe需要使用@Pipe装饰器,并实现PipeTransform接口里的transform方法。定义好的pipe要在需使用pipe的view或component中声明。
Pipe接口的定义
PipeDecorator
PipeTransform接口
-
pure 管道:仅当管道输入值变化的时候,才执行转换操作,默认的类型是 pure 类型。(备注:输入值变化是指原始数据类型如:string、number、boolean 等的数值或对象的引用值发生变化)
-
impure 管道:在每次变化检测期间都会执行,如鼠标点击或移动都会执行 impure 管道
<span style="color: #000000">@Pipe({
name: 'tempConvert'<span style="color: #000000">
})
<span style="color: #008000">//<span style="color: #008000"> The work of the pipe is handled in the tranform method with our pipe's class
<span style="color: #000000">class TempConvertPipe implements PipeTransform {
transform(value: number,args: any[]) {
<span style="color: #0000ff">if(value && !isNaN(value) && args[0] === 'celsius'<span style="color: #000000">) {
<span style="color: #0000ff">var temp = (value - 32) * 5/9;
<span style="color: #0000ff">var places = args[1<span style="color: #000000">];
<span style="color: #0000ff">return temp.toFixed(places) + ' C'<span style="color: #000000">;
}
</span><span style="color: #0000ff">return</span><span style="color: #000000">;
}
}
...
@View({
templateUrl: 'pipesTemplate.html'<span style="color: #000000">,<span style="color: #008000">//<span style="color: #008000"> We can pass the pipe class name into the pipes key to make it usable in our views
<span style="color: #000000"> pipes: [TempConvertPipe]
})
class PipesAppComponent {
temperature: number;
constructor() {
<span style="color: #0000ff">this.temperature = 85<span style="color: #000000">;
}
}
html
Custom Pipes - Convert Temperature
Fahrenheit: {{temperature + ' F'}}
Celsius: {{temperature | tempConvert:'celsius':1}}
结果
过滤出指定范围的值
定义一个pipe
<span style="color: #000000">@Pipe({
name: 'AgePipe'<span style="color: #000000">
})
export class AgePipe implements PipeTransform {
<span style="color: #008000">//<span style="color: #008000"> Transform is the new "return function(value,args)" in Angular 1.x
transform(value,args?<span style="color: #000000">) {
<span style="color: #008000">//<span style="color: #008000"> ES6 array destructuring
let [minAge,maxAge] =<span style="color: #000000"> args;
<span style="color: #0000ff">return input.filter(person =><span style="color: #000000"> {
<span style="color: #0000ff">return person.age >= +minAge && person.age <= +<span style="color: #000000">maxAge;
});
}
}
selector: 'playground-app'<span style="color: #000000">,templateUrl: 'app/playground.html'<span style="color: #000000">,<span style="color: #008000">//<span style="color: #008000"> tell our component it uses our AgePipe
<span style="color: #000000"> pipes: [AgePipe]
})
export class PlaygroundApp {
sliderValue:number = 20<span style="color: #000000">;
anotherSliderValue: number = 90<span style="color: #000000">;
people:Array
name: 'Justin Bieber'<span style="color: #000000">,age: 21<span style="color: #000000">
},{
name: 'Miley Cyrus'<span style="color: #000000">,age: 23<span style="color: #000000">
},{
name: 'John Legend'<span style="color: #000000">,age: 37<span style="color: #000000">
},{
name: 'Betty White'<span style="color: #000000">,age: 94<span style="color: #000000">
},{
name: 'Roger Waters'<span style="color: #000000">,age: 72<span style="color: #000000">
},{
name: 'Larry Page'<span style="color: #000000">,age: 42<span style="color: #000000">
}];
}
html
0 ="sliderValue"="sliderValue = $event.target.value" /> 100
{{ sliderValue }}0 ="anotherSliderValue" /> 100
{{anotherSliderValue }}