Call static function from angular2 template

后端 未结 4 2119
滥情空心
滥情空心 2020-12-05 06:22

I\'m trying to build \'utility\' services (classes) for an angular project. The utility classes have static functions (so we don\'t have to instantiate needless objects).

4条回答
  •  既然无缘
    2020-12-05 06:56

    There is already a pattern for doing this in Angular called pipes. In Angular 1 this was called filters.

    In Angular you create your custom pipe class and use the | in the template to pass values to. There is a built in one for dates, it is used like:

    {{ "2017-01-24" | date }}
    

    Of course if this pipe does not do what you want, you can create your own:

    @Pipe({
      name: 'myDate'
    })
    export class MyDate implements PipeTransform {
    
      transform(value: string): string {
        return new Date(value); 
      }
    }
    

    For more info please see: https://angular.io/docs/ts/latest/guide/pipes.html

提交回复
热议问题