0

我有一个这样的选择标签

 <select ng-model="teamId" 
         ng-options="team.globalId as team.name for team in teamList"
         ng-change="teamOnChange(teamId)">
 </select>

在团队更改中,我保留了 LocalStorage 中的值,并且在页面刷新时,我还设置了从 Localstorage 读取的 ng-model,但似乎没有帮助

我想在页面刷新时保留所选项目,我该怎么做

4

1 回答 1

2

您可以将您的信息存储到 LocalStorage 中,也可以使用服务。IE

保存在本地存储中

app.service('teamStorage', function() {

   var teamId;
   var teamName;

   //to save the data send it as a parameter obj
   this.saveTeam = function(data) {
        teamId = data.teamId;
        teamName = data.teamName;
        localStorage.setItem('teamInfo', JSON.stringify({
            teamName: teamName,
            teamId: teamId,
        }));
    };
   //to clear your localstorage
   this.clearTeam = function() {
        localStorage.removeItem('teamInfo');
        teamName = "";
        teamId = "";
    };

    //in case you want to get independent variables
    this.getTeamID = function() {
        return teamId;
    };

   this.getTeamName = function (){
        return teamName; 
    }
});

将服务注入您的控制器

app.controller('teamController', function($scope, teamStorage) {
         //you can access your localStorage on load like this

          var myLocalStorage = localStorage.getItem('teamInfo');
           $scope.save = function(teamId) {

              var data = {
               teamID:teamId,
               teamName: myTeam
              }
              //save team in LocalStorage
              teamStorage.saveTeam(data);

            };

           //clear team from LocalStorage
           teamStorage.ClearTeam();

           //get name of team
           $scope.myTeam = teamStorage.getTeamName();

});
于 2018-07-24T14:36:01.337 回答