angular 管道

217 阅读1分钟
管道将数据作为输入并将其转换为所需的输出(实现对数据的格式化)。

管道可以接受任意数量的可选参数来微调起输出,要向管道添加参数,请使用冒号(:)跟随管道名称,然后使用参数值,如:date: "MM/dd/yy",如果管道接受多个参数,请用冒号分割值如:slice:1:5。

<p>The hero's birthday is {{ birthday | date:"MM/dd/yy"}}</p>

birthday = new Date(1988, 3, 15);

<p>The hero's birthday is {{ birthday | date:format }}</p><button (click)="toggleFormat()">Toggle Format</button>
toggle = true; // start with true == shortDate  get format() {    return this.toggle ? 'shortDate' : 'fullDate';  }  toggleFormat() {    this.toggle = !this.toggle;  }

<p>The chained hero's birthday is{{ birthday | date:'fullDate' | uppercase}}</p>