javascript - Angular ng-repeat for a select, setting one option as selected using $rootScope variable -
i have following select:
<select class="form-control" onchange="user_changed_language()" id="headerlanguageselection" style="cursor:pointer;width:100%;height:30px;margin:0;padding:0" title="{{labels.change_language_tooltip}}"> <option ng-repeat="one_language in languages_list" value="{{one_language.code}}" ng-selected="one_language.code == current_language"> {{one_language.name}} </option> </select>
now, current_language
$rootscope
variable value (e.g. "en"). want select element display selected value instead of first. doing wrong?
one more note: know use ng-click
, don't think source of issue.
thanks.
check snippet:
var app = angular.module('app', []); app.controller('mainctrl', function($scope, $rootscope) { $scope.labels = {change_language_tooltip: "change lang"}; $scope.languages_list = [ { name: 'english', code: 'en' }, { name: 'espanol', code: 'es' }, { name: 'italian', code: 'it' }]; $rootscope.current_language = $scope.languages_list[1]; });
<!doctype html> <html ng-app="app"> <head> <meta charset="utf-8" /> <script src="https://code.angularjs.org/1.3.15/angular.js"></script> <script src="app.js"></script> </head> <body ng-controller="mainctrl"> <p>selected item : {{current_language}}</p> <select class="form-control" onchange="user_changed_language()" id="headerlanguageselection" style="cursor:pointer;width:100%;height:30px;margin:0;padding:0" title="{{labels.change_language_tooltip}}" ng-options="item.name item in languages_list track item.code" ng-model="current_language"> </select> </body> </html>
ps: selected item (default or initial) must 1 element of items used in ngoptions list
Comments
Post a Comment