Angular - Use pipes in services and components

前端 未结 8 1556
南笙
南笙 2020-11-22 09:42

In AngularJS, I am able to use filters (pipes) inside of services and controllers using syntax similar to this:

$filter(\'date\')(myDate, \'yyyy-MM-dd\');
         


        
8条回答
  •  不知归路
    2020-11-22 10:30

    If you don't want do 'new myPipe()' because you're injecting dependencies to pipe, you can inject in component like provider and use without new.

    Example:

    // In your component...
    
    import { Component, OnInit } from '@angular/core';
    import { myPipe} from './pipes';
    
    @Component({
      selector: 'my-component',
      template: '{{ data }}',
      providers: [ myPipe ]
    })
    export class MyComponent() implements OnInit {
      data = 'some data';
      constructor(private myPipe: myPipe) {}
    
      ngOnInit() {
        this.data = this.myPipe.transform(this.data);
      }
    }
    

提交回复
热议问题