Using comma as a list separator in Angular 2

后端 未结 4 1119
一生所求
一生所求 2020-12-08 18:39

I want to create a list of items in my template, separated by commas, but I don\'t want the last item to have a comma:

one, two, three

How

4条回答
  •  一生所求
    2020-12-08 18:55

    I have just needed todo this but my value is in an attribute so the marked answer did not work.

    Someone may find this helpful :

    I used a pipe, I could have built the string up but felt a pipe would be more readable

    CLI

    ng g p Delimiter
    

    Pipe

    import { Pipe, PipeTransform } from '@angular/core';
    @Pipe({
      name: 'delimiter'
    })
    export class DelimiterPipe implements PipeTransform {
      transform(value: string, delimiter: string, isLast: boolean)  {
        return !isLast ? value + delimiter : value;
      }
    }
    

    View

    I needed some html in my view hence my need to use the attribute, eg

提交回复
热议问题