【问题标题】:angular2 rc6 custom global pipes; PLATFORM_PIPESangular2 rc6 自定义全局管道; PLATFORM_PIPES
【发布时间】:2016-09-13 13:53:26
【问题描述】:

我在旧版本中使用 Angular 2.0.0-rc.6 版本,有可能创建一个全局管道并像 here 和许多其他在线位置显示的那样注册它。

我正在尝试在我的应用中做类似的事情,但我遇到了一个问题, 我无法从 angular/core 导入 PLATFORM_PIPES,而且我还了解到 documentation 和其他在线位置已弃用它。

我在这件事上找到了这个Q&A,但它对我没有帮助,因为我正在使用@NgModule,而且我似乎找不到以任何方式将管道放入其中的方法。

有什么想法吗?

【问题讨论】:

    标签: angular typescript import


    【解决方案1】:

    默认管道和自定义管道的工作演示:

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


    默认管道

    你不需要为此做任何事情。

    由于 PLATFORM_PIPES 已被弃用,在 RC6 中,默认管道默认情况下可用。

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

    自定义管道

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

    我想在 AppComponent 中使用它

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

    .html

     {{ your data/value | awesome }}
    

    【讨论】:

    • 我说的是自定义全局类型,而不是默认类型。
    • 这是我的问题,如何注册?
    • 好的,非常感谢,这正是我所需要的!
    • 我还用两种类型的管道更新了我的 plunker 链接。
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 2017-03-20
    • 2016-06-18
    • 2017-01-17
    • 1970-01-01
    • 2017-03-09
    • 2017-04-29
    • 1970-01-01
    相关资源
    最近更新 更多