0

I'd like to have multiple default selected option in AngularJs.

In my controller code:

  $scope.selectedFvs = ['fvid9', 'fvid2'];

  $scope.fields = [{
    id: 'fid1',
    name: 'f name 1',
    fieldValues: [{
      id: 'fvid1',
      name: 'fv name 1',
    }, {
      id: 'fvid2',
      name: 'fv name 2'
    }]
  }, {
    id: 'fid2',
    name: 'f name 2',
    fieldValues: [{
      id: 'fvid3',
      name: 'fv name 3',
    }, {
      id: 'fvid4',
      name: 'fv name 4'
    }, {
      id: 'fvid5',
      name: 'fv name 5',
    }, {
      id: 'fvid6',
      name: 'fv name 6'
    }]
  }, {
    id: 'fid3',
    name: 'f name 3',
    fieldValues: [{
      id: 'fvid7',
      name: 'fv name 7',
    }, {
      id: 'fvid8',
      name: 'fv name 8'
    }, {
      id: 'fvid9',
      name: 'fv name 9',
    }]
  }];

In HTML code:

  <div ng-repeat="field in fields">
    <label>{{::field.name}}</label>
    <select ng-init="fields[field.id] = selectedFvs[1]" ng-model="fields[field.id]">
      <option value="select-all">Select All</option>
      <option ng-repeat="fieldValue in field.fieldValues" value="{{::fieldValue.id}}">{{fieldValue.name}}</option>
    </select>
  </div>

This renders with 'fv name 5' as default selected field value because in the HTML code, the select tag, the ng-init is set to the second index of selectedFvs:

Render HTML 1

However, this is not what I want. What I really want is the 3 selects have the default value of selectedFvs. It would look like this if it works,

enter image description here

Here is the Plunker link to this code.

1
  • Before edited $scope.selectedFvs = ['fvid2', 'fvid5', 'fvid9'], and after edited $scope.selectedFvs = ['fvid9', 'fvid2']; I get the $scope.selectedFvs = ['fvid2', 'fvid5', 'fvid9'] to work by changing selectedFvs[1] in the HTML code to selectedFvs[$index]. But when $scope.selectedFvs = ['fvid9', 'fvid2'], I'm stuck. Commented Nov 6, 2015 at 11:56

2 Answers 2

2

Use $index as follows

<div ng-repeat="field in fields">
    <label>{{::field.name}}</label>
    <select ng-init="fields[field.id] = (selectedFvs|findselected: field.fieldValues)" ng-model="fields[field.id]">
      <option value="select-all">Select All</option>
      <option ng-repeat="fieldValue in field.fieldValues" value="{{::fieldValue.id}}">{{fieldValue.name}}</option>
    </select>
  </div>

Uae this filter as well

app.filter("findselected", function() { // register new filter

  return function(inputs, fields) { // filter arguments
    var selected = '';
    angular.forEach(inputs, function(val1){
         angular.forEach(fields, function(val2){
           if(val1 === val2.id){
             selected = val1;
           }
         })
    });
    return selected;
  };
});

See this Plunker

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's working, I just recently ran up to a new issue when I change the $scope.selectedFvs to $scope.selectedFvs = ['fvid9', 'fvid2']; The expected answer looks like the last image in the edited question.
@Vicheanak Then create a new filter for that. Updated the answer. If it works accept the answer and upvote also. :)
1

Use selectedFvs[$index] instead of selectedFvs[1]

5 Comments

Awesome! Working great.
What if the $scope.selectedFvs = ['fvid2', 'fvid5', 'fvid9'] is changed to $scope.selectedFvs = ['fvid9', 'fvid2'];
Sorry, misunderstood a little. You need some filter, and I guess @Partha already give a right solution :)
I was the one who explained incorrectly. Thank you so much for your effort.
You are welcome, always glad to help, but actually I didn't do anything :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.