Angular ui -router页面浏览器刷新显示空值?如何保留数据?

时间:2016-05-26 14:37:57

标签: javascript angularjs angular-ui-router angular-routing

我正在使用Angular Js Ui路由器,当点击按钮时,我已调用该函数并接管params并调用该服务并从Db获取结果。工作得很好。

在时间点。

当浏览器刷新页面时,该值变为null并且respone得到500错误。

它查看状态配置路径并转到控制器,有空值。 如何在ui-state提供者中保留数据。

Url Look like :
http://localhost/Myproject/#/home/Retailer/retailerId=1?retailerCode=3

When I click the button i have carry over the value
 $state.go('Home.retailer', { 'retailerId': $scope.param1, 'retailercode': $scope.param1});



While refersh the page, the values getting null in module.

.state('home.retailer', {

        url: '/contract/:retailerId:9?retailercode:1',
        params:{
                   'retailerId': '9', 'retailercode': '1' 
       },
        views: {
            '@': {
                templateUrl: baseUrl + 'retailr/retailerList',
                controller: 'home',
            }
        },
        data: {
           Name: '',
            Img: 'Images/1.png'
        }

    })

注意: 当按钮点击我已获得值时,服务将返回数据。

问题: 刷新页面时,它进入模块并检查home.retailer,然后返回服务。如何在刷新浏览器时保留数据。

这到底能做什么:params:{                        'retailerId':'9','零售商代码':'1'            },

Because of null, response Here I am passing the retailer id and Retailer code in service inside the controller. 
 `RetailerService.pageLoad($scope.retailerId,$scope.retailercode)` when Click the button ,
RetailerService.pageLoad($scope.retailerId :9 ,$scope.retailercode :1) , 



Page Refresh RetailerService.pageLoad($scope.retailerId : null,$scope.retailercode : null)` 

1 个答案:

答案 0 :(得分:4)

要在刷新时保留数据,您需要将其存储在浏览器存储中。这是使用localStorage的工厂的一个很好的例子。

app.factory("storageFactory", function($window) {
  return {
    setData: function(val) {
      $window.localStorage && $window.localStorage.setItem('my-storage', val);
      return this;
    },
    getData: function() {
      return $window.localStorage && $window.localStorage.getItem('my-storage');
    }
  };
});

因此,您将在控制器中的不同时间设置数据,然后在页面加载时调用get data以确保捕获任何预设的变量。

编辑:如下所述,而不是localStorage,sessionStorage也是一个选项。你选择的应该取决于你的需求。