简体   繁体   English

如何传递逗号分隔的字符串以选择Angular js中的选项

[英]How to pass a Comma seperated String to select options in Angular js

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 comma separated string Please help me.. 如何将选项传递给选择选项,因为它是逗号分隔的字符串,请帮助我。

Assuming that you have got you data in $scope.data 假设您已经在$ 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 或使用ng-options

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

</select>

or you can make a filter for it 或者您可以为其过滤

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

In view 视野中

<select name="yourinput">
<option ng-repeat="o in data.props.options | commaspliter"></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:'|'" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM