Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected for select option menu if condition is met in AngularJS

Tags:

angularjs

<select ng-model="item.value" ng-options="item.name for item in items">
</select>

The above will populate select option in AngularJS, but how can I add selected if my condition is met?

I want to do something like this:

<select ng-model="item.value" ng-options="item.name for item in items" if="item.name == someValueToCheckAgainst" selected endif>
</select>

Obviously the above is wrong, but I was trying to search for this to see if it is possible to do.

This is items

var items = [ 'a', 'b', 'c' ];
var someValueToCheckAgainst = 'b';

so my menu should be like this

<select ng-model="item.value">
  <option value="a">a</option>
  <option value="b" selected>a</option>
  <option value="c">a</option>
</select>
like image 762
Ali Avatar asked Nov 29 '22 00:11

Ali


1 Answers

Just figured this out

<select ng-model="form.model">
  <option ng-selected="{{item == valueToCheckAgainst}}"
          ng-repeat="item in items"
          value="{{item}}">
          {{item}}
  </option>
</select>
like image 153
Ali Avatar answered Dec 01 '22 14:12

Ali