Angular2 split string (pipe?)

后端 未结 2 1198
你的背包
你的背包 2021-01-06 16:10

Is it possible to use something similar to {{mystr | split(last) }} somehow?

I\'m hoping there is a pipe included already.

2条回答
  •  别那么骄傲
    2021-01-06 16:42

    You need a custom pipe for this. You could implement this this way:

    @Pipe({
      name: 'split'
    })
    export class SplitPipe implements PipeTransform {
      transform(val:string, params:string[]):string[] {
        return val.split(params[0]);
      }
    }
    

    and use it like this:

    {{mystr | split:last }}
    

    where last is a property of your component that corresponds to the separator.

提交回复
热议问题