Angular 2 - 使用管道

时间:2017-02-15 11:43:09

标签: angular pipe

我正在尝试使用管道中的typescript将数字值转换为角度为2的字符串。抱怨

  

类型字符串不能分配给类型编号

。我的管道如下。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'pxsuffix'

}) export class pxsuffix implements PipeTransform {

transform(input: number): number {

if ((input > 0)) {
    input = input.toString(),
}

return (
    input = input + 'px',

);
}
}

1 个答案:

答案 0 :(得分:7)

您的函数要求返回一个数字,并且您正在返回一个字符串。尝试:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'pxsuffix'

}) export class pxsuffix implements PipeTransform {

transform(input: number): string{ //string type
   return input + 'px';
} }