The value attribute returns strings and the value.id is a number. Use ng-value without curly bracket {{ }} interpolation.
<select ng-model="form.language_id" name="language_id" required >
<option ng-repeat="(key,value) in langs" ̶v̶a̶l̶u̶e̶=̶"̶{̶{̶v̶a̶l̶u̶e̶.̶i̶d̶}̶}̶
ng-value="value.id">
{{value.languageTitle}}
</option>
</select>
From the Docs:
In general, the match between the model and an option is evaluated by strictly comparing the model value against the value of the available options.
If you are setting the option value with the option's value attribute, or textContent, the value will always be a string which means that the model value must also be a string. Otherwise the select directive cannot match them correctly.
To bind the model to a non-string value, you can use one of the following strategies:
- the
ngOptions directive (select)
- the
ngValue directive, which allows arbitrary expressions to be option values (Example)
- model $parsers / $formatters to convert the string value (Example)
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.form={"language_id":2};
$scope.langs=[
{"id":1,"languageTitle":"test1"},
{"id":2,"languageTitle":"test2"},
{"id":3,"languageTitle":"test3"},
]
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<select ng-model="form.language_id" name="language_id" required >
<option ng-repeat="(key,value) in langs" ng-value="value.id">
{{value.languageTitle}}
</option>
</select>
</body>
For more information, see
ng-optionsinstead ofng-repeatto create the <option> elements$scope.form.language_id="2".