2017-06-18 99 views
0

我要創造我的離子設置應用頁面離子localStorage的選擇

HTML:

<select class="selectCountry"> 
    <option value="fr">France</option> 
    <option value="usa">USA</option> 
</select> 

Controler.js

angular.module('starter.controllers', ['ionic', 'ngStorage']) 

.controller('SettingsCtrl',function($scope, $localStorage){ 
// what to do to save and data from <select> & use if condition 
}); 

回答

0

你可以把ng-modelselect並觸發onchange函數

<select class="selectCountry" ng-model="selectCountry" ng-change="save()"> 
     <option value="fr">France</option> 
     <option value="usa">USA</option> 
    </select> 

,並在控制器

 angular.module('starter.controllers', ['ionic', 'ngStorage']) 

      .controller('SettingsCtrl',function($scope, $localStorage){ 
      // what to do to save and data from <select> & use if condition 
    $scope.save = function(){ 
    $localStorage.selectCountry= $scope.selectCountry; // set 
alert($localStorage.selectCountry); // get 
    } 

     $localStorage.selectCountry = $localStorage.selectCountry; // get 
      }); 

檢查它NgStorage

的文檔我創造了這個筆Here

+0

感謝您的幫助,但它沒有奏效。 的ReferenceError:保存不定義 – HassanYorke

+0

不工作 的ReferenceError:保存沒有定義 – HassanYorke

+0

在平變化問題=「保存()」 – HassanYorke

0
.service('storage', function() { 
     var myjsonObj = "nothing";//the object to hold our data 
    return { 
    setJson:function(name, item){ 
      window.localStorage.setItem(name, item); 


    }, 
    getJson:function(name){ 
     return window.localStorage.getItem(name); 
    } 
    } 
    }) 

,如果你想存儲和檢索的變量。

storage.setJson("nameOfSetting", "theVariableYouWannaStore"); 
var theVariableYouStored = storage.getJson("nameOfSetting");