要在HTML模板中指定值转换,请使用管道运算符(|)。
{{interpolated_value | pipe_name}}
您可以链接管道,发送一个管道功能的输出以被另一个管道功能转换。管道还可以使用参数来控制其执行转换的方式。例如,您可以将所需的格式传递给date管道。
<!-- Default format: output ‘Jun 15, 2015‘-->
<p>Today is {{today | date}}</p>
<!-- fullDate format: output ‘Monday, June 15, 2015‘-->
<p>The date is {{today | date:‘fullDate‘}}</p>
<!-- shortTime format: output ‘9:43 AM‘-->
<p>The time is {{today | date:‘shortTime‘}}</p>
? ?
来自 <https://angular.io/guide/architecture-components#pipes>
? ?
例
@NgModule({
imports: [
BrowserModule,
ReactiveFormsModule,
RouterModule.forRoot([
{ path: ‘‘, component: ProductListComponent },
{ path: ‘products/:productId‘, component: ProductDetailsComponent },
])
],
例
<div *ngFor="let product of products; index as productId">
? ?
<h3>
<a [title]="product.name + ‘ details‘" [routerLink]="[‘/products‘, productId]">
{{ product.name }}
</a>
</h3>
<!-- . . . -->
</div>
the route (URL) 包含一个常量 (/products) and 一个变量productId,会动态地插入当前产品的id
import { Component, OnInit } from ‘@angular/core‘;
import { ActivatedRoute } from ‘@angular/router‘;
? ?
import { products } from ‘../products‘;
export class ProductDetailsComponent implements OnInit {
product;
? ?
constructor(
private route: ActivatedRoute,
) { }
}
<h2>Product Details</h2>
? ?
<div *ngIf="product">
<h3>{{ product.name }}</h3>
<h4>{{ product.price | currency }}</h4>
<p>{{ product.description }}</p>
? ?
</div>
? ?
参考自 <https://angular.io/start/routing>
? ?
? ?
? ?
原文:https://www.cnblogs.com/PomeloYe/p/11807069.html