如何在下拉Angular JS中将本地存储值设置为选定值?

时间:2018-01-10 12:59:37

标签: angularjs local-storage angular-material

我有一个Angular JS材料下拉菜单,项目正从我的控制器加载。当用户点击某个项目时,我将其值存储到本地存储。它运作良好。选择值后,我想将该(本地存储值)值设置为下拉默认值。

  <div flex>
    <md-select ng-model="$callcenterController.selectedLanguage" ng-selected="$callcenterController.getLocalStorageItem" ng-change="$callcenterHomeController.languageChange($callcenterController.selectedLanguage.name)"  placeholder="{{$callcenterController.selectLanguageLabel}}" >
        <md-option ng-value="languages" ng-repeat="languages in $callcenterHomeController.languageList"></md-select-label>  {{ languages.name }}</md-option>
  </md-select>    

来自我的控制器

ctrl.getLocalStorageItem = localStorage.getItem('selectLanguage')

ctrl.languageChange = function (key) {
if (typeof this.selectedLanguage != 'undefined'){

  localStorage.setItem('selectLanguage', key);

  $route.reload();

}  }

1 个答案:

答案 0 :(得分:1)

您必须将值设置为在ng-model中绑定的变量。在你的情况下

$callcenterController.selectedLanguage

所以:

Controller:

ctrl.selectedLanguage = localStorage.getItem('selectLanguage');

ctrl.languageChange = function () {
  if(ctrl.selectedLanguage) {
    localStorage.setItem('selectLanguage', ctrl.selectedLanguage);
  }
}

查看:

<md-select ng-model="$callcenterController.selectedLanguage"  ng-change="$callcenterHomeController.languageChange()" >
...etc
  </md-select>