admin管理员组

文章数量:1404923

I have this Json:

        {
                "field_id": "4otBG",
                "label": "Fullness",
                "type": "dropdown",
                "props": {
                    "req": "true",
                    "options": "Empty-ish, Plenty of Seats, Few Seats, Standing, Packed Like Sardines"
                }
            }

How to pass the Options to an selection option , because it is a ma separated string Please help me..

I have this Json:

        {
                "field_id": "4otBG",
                "label": "Fullness",
                "type": "dropdown",
                "props": {
                    "req": "true",
                    "options": "Empty-ish, Plenty of Seats, Few Seats, Standing, Packed Like Sardines"
                }
            }

How to pass the Options to an selection option , because it is a ma separated string Please help me..

Share Improve this question edited Dec 23, 2014 at 20:33 A.B 20.5k3 gold badges42 silver badges73 bronze badges asked Dec 23, 2014 at 18:18 Parvindra SinghParvindra Singh 532 silver badges10 bronze badges 1
  • Repeated stackoverflow./questions/19718609/split-for-ng-repeat-item – Carlos Verdes Commented Dec 23, 2014 at 18:44
Add a ment  | 

2 Answers 2

Reset to default 5

Assuming that you have got you data in $scope.data

$scope.optionsplit =  $scope.data.props.options.split(',');

In view

<select name="yourinput">
   <option ng-repeat="o in optionsplit" value="{{o}}">{{o}}</option>
<select>

Or with ng-options

<select ng-options="o for o in optionsplit">

</select>

or you can make a filter for it

filter('maspliter', function() {
    return function(input) {
    return input.split(',');
    } 
  });

In view

<select name="yourinput">
<option ng-repeat="o in data.props.options | maspliter"></option>
 </select>

The angular way would be to use a filter. Note you're not supposed to use $ prefix, but since this is such a general purpose filter I thought it was appropriate. Feel free to rename.

.filter('$split', function() {
  return function(val, splitter) {
    if (!splitter) { splitter = ','; }
    return (val && val.length) ? val.split(splitter) : [];
  }
});

Use it like so.

<select ng-options="val for val in props.options | $split" />

You can even pass an additional param if you have data that is delimited by a different character.

<select ng-options="val for val in props.options | $split:'|'" />

本文标签: javascriptHow to pass a Comma seperated String to select options in Angular jsStack Overflow