我正在尝试limitTo
在字符串上的 Angular2 上运行管道:
{{ item.description | limitTo : 20 }}
我收到以下错误:
The pipe 'limitTo' could not be found
是否有可能在 Angular2 中删除了该管道?
这是我的app.module
从'./limit-to.pipe'导入{TruncatePipe};
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService),
RouterModule.forRoot([
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'home',
component: GridComponent
},
])
],
declarations: [
AppComponent,
TopNavComponent,
GridComponent,
TruncatePipe
],
providers: [
PinService,
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我使用管道的网格组件:
import { Component,OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
moduleId : module.id,
selector: 'my-grid',
templateUrl : 'grid.component.html',
styleUrls: [ 'grid.component.css']
})
export class GridComponent implements OnInit{
constructor(
private router: Router,
private gridService: GridService) {
}
ngOnInit(): void {
}
}
我的管道定义:
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({
name: 'limitToPipe'
})
export class TruncatePipe implements PipeTransform {
transform(value: string, limit: number) : string {
let trail = '...';
return value.length > limit ? value.substring(0, limit) + trail : value;
}
}
最后是我的模板:
<div *ngFor="let item of items" class="grid-item">
<p class="simple-item-description">
{{ item.description | limitToPipe : 20 }}
</p>
</div>