简体   繁体   English

angular2 rc6自定义全局管道; PLATFORM_PIPES

[英]angular2 rc6 custom global pipes; PLATFORM_PIPES

I am using angular with version 2.0.0-rc.6 in older versions there was a possibility to create a global pipe and register it like is shown here and in many other locations online. 我在旧版本中采用了棱角分明的版本2.0.0-rc.6有建立一个全球性的管道,并将其注册喜欢所示的可能性在这里和其他许多地方网上。

I am trying to do something similar in my app but there is a problem I encountered, I can't import PLATFORM_PIPES from angular/core and I also read that it is deprecated on the documentation and other places on line. 我正在尝试在我的应用程序中执行类似的操作,但是遇到了一个问题,我无法从angular / core导入PLATFORM_PIPES,并且我还阅读到文档和其他在线位置已弃用它。

I found this Q&A on the matter, but it did not help me since I am using @NgModule and I can't seem to find a way to put the pipe in it in any way. 我在此问题上找到了问与答 ,但是由于我使用@NgModule,因此它无济于事,而且我似乎也找不到以任何方式将管道放入其中的方法。

Any ideas? 有任何想法吗?

Working Demo of Default-Pipe & Custom-Pipe: 默认管道和自定义管道的工作演示:

https://plnkr.co/edit/BmZrCbl0czJwnPMf0x0V?p=preview https://plnkr.co/edit/BmZrCbl0czJwnPMf0x0V?p=preview


Default pipe 默认管道

You don't need to do anything for this. 您不需要为此做任何事情。

Since PLATFORM_PIPES has been deprecated, in RC6 , default pipes are available by default . 由于PLATFORM_PIPES已被弃用,因此在RC6中默认管道default可用

 import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` {{mydate | date:"MM/dd/yy"}}` }) export class AppComponent { mydate = Date.now(); } 

Custom pipe 定制管

 import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'awesome' }) export class AwesomePipe implements PipeTransform { transform(phrase: string) { return phrase ? 'Awesome ' + phrase : ''; } } 

I want to use it in AppComponent 我想在AppComponent中使用它

  @NgModule({ imports: [ BroswerModule, FormsModule ], declarations: [ AppComponent, AwesomePipe ], //<----added here exports: [ AppComponent], providers: [ ] }) 

.html .html

  {{ your data/value | awesome }} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM