Split for ng repeat item?

前端 未结 3 850
一生所求
一生所求 2021-02-20 04:18

Simple question- if I have this in my controller:

$scope.somedata = \'Item 1, Item 2\'; // set from something else

is there a way to split some

3条回答
  •  别那么骄傲
    2021-02-20 05:20

    The string the right of the | is resolved to the name of a filter (usually they are string formatting filters, but the angular team has also provided the filter filter, which returns a filtered array (a bit misleading because the two share the same name). You could create your own split filter to accomplish what you want:

    angular.module('myFilters', []).
      filter('split', function() {
        return function(input, delimiter) {
          var delimiter = delimiter || ',';
    
          return input.split(delimiter);
        } 
      });
    

    You could use the filter like so:

    Or specify a delimiter:

    Consider the above pseudo-code because I haven't tested it.

    Le plunker: http://plnkr.co/edit/hk6F0Y6p5YAXv6fyXfSz?p=preview

提交回复
热议问题