AngularJs - ng-model in a SELECT
You can use the ng-selected directive on the option elements. It takes expression that if truthy will set the selected property.
In this case:
<option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
Demo
angular.module("app",[]).controller("myCtrl",function($scope) { $scope.units = [ {'id': 10, 'label': 'test1'}, {'id': 27, 'label': 'test2'}, {'id': 39, 'label': 'test3'}, ] $scope.data = { 'id': 1, 'unit': 27 }});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><div ng-app="app" ng-controller="myCtrl"> <select class="form-control" ng-change="unitChanged()" ng-model="data.unit"> <option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option> </select></div>
try the following code :
In your controller :
function myCtrl ($scope) { $scope.units = [ {'id': 10, 'label': 'test1'}, {'id': 27, 'label': 'test2'}, {'id': 39, 'label': 'test3'}, ]; $scope.data= $scope.units[0]; // Set by default the value "test1" };
In your page :
<select ng-model="data" ng-options="opt as opt.label for opt in units "> </select>
You dont need to define option tags, you can do this using the ngOptions directive: https://docs.angularjs.org/api/ng/directive/ngOptions
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit" ng-options="unit.id as unit.label for unit in units"></select>