Angular2 Pipe
AngularJs 1.x 中使用来帮助我们转换templates中的输出,但在Angular2中使用的是,以下展示Angular 1.x and Angular 2中filter和pipe的对比:
<span style="color: #008000">// <span style="color: #008000"><span style="color: #000000"> import {Component,View,bootstrap} from 'angular2/angular2'<span style="color: #000000">; @Component({ constructor() { bootstrap(PipesAppComponent); Dates{{date | date:'mediumDate'}} {{date | date:'yMMMMd'}} {{date | date:'shortTime'}} 结果: New Pipesdecimal和percent是Angular2中新增的管道,参数规则是: decimal管道在模板中使用number关键字
@View({
templateUrl: 'pipesTemplate.html'<span style="color: #000000"> }) class PipesAppComponent { 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
@Component({
selector: 'pipes'<span style="color: #000000">,changeDetection: 'ON_PUSH'<span style="color: #000000"> }) @View({ templateUrl: 'pipesTemplate.html'<span style="color: #000000">,}) class PipesAppComponent { constructor() { ... 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 export const Pipe: PipeDecorator =
PipeTransform接口
'angular2/angular2'...
<span style="color: #008000">//<span style="color: #008000"> We use the @Pipe decorator to register the name of the pipe
<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">; }
} ... @View({ class PipesAppComponent { constructor() { html Custom Pipes - Convert TemperatureFahrenheit: {{temperature + ' F'}}Celsius: {{temperature | tempConvert:'celsius':1}}结果 过滤出指定范围的值 定义一个pipe import {Pipe, PipeTransform} from 'angular2/core'<span style="color: #008000">//<span style="color: #008000"> Tell Angular2 we're creating a Pipe with TypeScript decorators
<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 } import {Component} from 'angular2/core''./pipes/age-pipe/age-pipe'@Component({
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 =<span style="color: #000000"> [{ html 0 ="sliderValue"="sliderValue = $event.target.value" /> 100 {{ sliderValue }}0 ="anotherSliderValue" /> 100 {{anotherSliderValue }}(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |