beshoy semsem beshoy semsem - 1 year ago 89
AngularJS Question

Bind from ngstorage to dropdown

I used ngstorage to save some value in localstorage
My contrroller is:

app.controller("main", function( $scope, $localStorage) {
$scope.$storage = $localStorage.$default({
selectedlanguage: { language: null, text: "All Languages" }
$scope.languages = [
{ language: null, text: "All Languages" },
{ language: "en", text: "English" },
{ language: "ar", text: "Arabic" }


My view is:

<select ng-model="$storage.selectedlanguage" ng-options="lang.text for lang in languages">

when reload the page angular can not bind the selectedlanguage
Thanks in advance

Answer Source

In the ng-options attribute, you can specify the values and title for each option in the expression it self, your other issue was that you're specifying an object as the ng-model, you need to tell the dropdown which value to match on, so accessing language on the selectedlanguage object will work fine.

<select ng-model="$storage.selectedlanguage.language" ng-options="lang.language as lang.text for lang in languages">

The above is the same as below, but a more complex expression, the below is just easier to read but it's exactly the same:

<select ng-model="$storage.selectedlanguage.language">
    <option ng-repeat="lang in languages" value="{{lang.language}}">


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download