Angular - Use pipes in services and components

前端 未结 8 1616
南笙
南笙 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:23

    This answer is now outdated

    recommend using DI approach from other answers instead of this approach

    Original answer:

    You should be able to use the class directly

    new DatePipe().transform(myDate, 'yyyy-MM-dd');
    

    For instance

    var raw = new Date(2015, 1, 12);
    var formatted = new DatePipe().transform(raw, 'yyyy-MM-dd');
    expect(formatted).toEqual('2015-02-12');
    

提交回复
热议问题