I am trying to set default value of options I am using select and ng-repeat. I do get data in js file and I do call model to set value there.
Please have a look for below code :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-ngrepeat-select-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="ngrepeatSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="repeatSelect"> Repeat select: </label>
<select ng-model="datamodel">
<option ng-repeat="dataitem in data"
ng-selected ="{{dataitem == datamodel}}">{{dataitem}}</option>
</select>
</form>
<hr>
<tt>repeatSelect = {{datamodel}}</tt><br/>
</div>
</body>
</html>
app.js file
(function(angular) {
'use strict';
angular.module('ngrepeatSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = [1,2,3,4,5];
$scope.datamodel = 2;
}]);
})(window.angular);
Here is my plunker
From the angular docs:
So changing
$scope.datamodel = 2;
to$scope.datamodel = '2';
works. See updated plunker.However, it's better to use
ngOptions
instead. Again, from the docs:So to keep your model as an integer, you can rather use:
See plunker