0

I have a model (param) with properties:

  • param.ValidValues (array of key-value pair)
  • param.DefaultValues (array of value strings)

I am rendering multi-select dropdown

<select multiple ng-model="param.DefaultValues">
    <option ng-repeat="item in param.ValidValues" value="{{item.Value}}" label="{{item.Key}}" 
        ng-selected="???">
    </option>
</select>

Question: How can I (using AngularJS markup syntax) set default values in this dropdown (pre-select options that match whatever is in param.DefaultValues) ?? Use ng-selected or there is another option?

For example:

if ValidValues = ["one", "two", "three", "four"] and DefaultValues = ["two", "three"]

then "two", "three" should be pre-selected. Makes sense?

Please note, I cant modify param.ValidValues collection.

monstro
  • 6,254
  • 10
  • 65
  • 111
  • 1
    On your controller you should save the value inside the select's `ngModel` - `$scope.param.DefaultValues = ['firstDefaultItemValue', 'secondDefaultItemValue'];` – Alon Eitan Jan 26 '17 at 20:37
  • @Alon Eitan, sorry, didnt get that. DefaultValues used to initialise the control, but also used as a model for user's selection – monstro Jan 26 '17 at 20:57

2 Answers2

2
<div ng-controller="myCtrl as $ctrl">    
  <select multiple ng-model="$ctrl.selectedValues"
    ng-options="o.value as o.title for o in $ctrl.languages">
    <option value=""></option>
  </select>
  <br>
  {{$ctrl.selectedValues}}
</div>

app.js file

var app = angular.module('app', []);
app.controller('myCtrl', function ($scope) {
this.languages = [
  {value: 'C#', title : 'C Sharp'},
  {value: 'PHP', title : 'p h p'},
  {value: 'Go', title : 'Go javascript'},
  {value: 'ES6', title: 'javascript'},
  {value: 'R', title: 'Rust'}
]   

this.selectedValues = ["C#","Go","R"];
});
YuryDG
  • 425
  • 3
  • 7
0

try this stackoverflow.com

<select multiple ng-model="param.DefaultValues" ng-init="param.DefaultValues = item[0]">
<option ng-repeat="item in param.ValidValues" value="{{item.Value}}"    label="{{item.Key}}" 
    ng-selected="???">
</option>
</select>
Community
  • 1
  • 1
ambussh
  • 740
  • 1
  • 7
  • 18