set option value as array

1000 views Asked by At

Setting up some permissions. I'd like to be able to set my item.roles as an array. Is this possible? If not what's the smartest way to go about getting my item.roles model to be an array in the end?

  <select class="form-control" ng-model="item.roles" ng-init="item.roles[0]='client'">
    <option value="[super, admin, producer, resource, client]">Super</option>
    <option value="[admin, producer, resource, client]">Admin</option>
    <option value="[producer, resource, client]">Producer</option>
    <option value="[resource, client]">Resource</option>
    <option value="[client]">Client</option>
 </select>
1

There are 1 answers

0
dfsq On BEST ANSWER

You can use ngList directive:

<select class="form-control" ng-list ng-model="item.roles" ng-init="item.roles=['client']">
    <option value="super, admin, producer, resource, client">Super</option>
    <option value="admin, producer, resource, client">Admin</option>
    <option value="producer, resource, client">Producer</option>
    <option value="resource, client">Resource</option>
    <option value="client">Client</option>
</select>

Note, that option values in this case are comma separated list (or you can change delimiter).

ngList directive is used to convert delimited list of values to/from array:

Text input that converts between a delimited string and an array of strings. The default delimiter is a comma followed by a space - equivalent to ng-list=", ". You can specify a custom delimiter as the value of the ngList attribute - for example, ng-list=" | ".

Demo: http://plnkr.co/edit/TbkEne7ap4WpCS4UPj7U?p=preview