【问题标题】:ionic localStorage select离子本地存储选择
【发布时间】:2017-06-18 20:57:51
【问题描述】:

我想在我的 ionic 应用中创建设置页面

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 
});

【问题讨论】:

    标签: ionic-framework local-storage


    【解决方案1】:

    您可以将ng-model 放在select 上并触发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

    【讨论】:

    • 感谢您的帮助,但没有奏效。 ReferenceError:保存未定义
    • onchange="save()"中的问题
    • @HassanYorke 它仍然具有大写 S 的保存功能,请再次检查代码。
    【解决方案2】:
    .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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2020-12-28
      • 2017-02-07
      • 2021-05-15
      • 2016-08-10
      相关资源
      最近更新 更多