2016-05-17 139 views
1

我正在使用AngularJs构建Web应用程序。我有一个搜索页面。每次用户磁带查询我从后端获取数据并更新url使用$ location.search() service.When用户点击刷新按钮我想保存视图的状态(同一个查询)。我使用$ location.search()来获取当前查询,但它返回一个空对象和控制器重新加载,所以即使范围已经消失(包含用户查询的范围)。我正在使用ng-route并配置reloadOnsearch = false。如何使用$ location来处理URL更改和刷新按钮。搜索()服务?

app.controller('resultCrtl',function($scope,myService,$http,$location,$window) { 
/*guetting query from previous view*/ 
$scope.userquery=myService.get(); 

    var uri; 
/*testing the refresh */ 
    if(!myService.get()) 
{ 
var loc=$location.search('query'); 
    $window.alert(loc); 
    /* it returns an empty object*/ 
    } 
else { 

    $http({ 
     method : 'GET', 
     url : '/response/' + $scope.userquery 
    }).success(function(data) { 
     $scope.result = data; 
     uri=encodeURI($scope.userquery); 
     $location.search('query',uri); 
    }).error(function(data, status, headers, config) { 
     alert("failure"); 
    }); 
$scope.search=function(){ 
    /* same code to get data and update the url */ 
    } 
    }); 

回答

0

很明显,刷新后它会变成空白。 '$ location.search('query',uri);'只会更改或更新浏览器的地址栏。所以刷新后,你可以使用$ stateParams.query从浏览器地址栏中获取这个值,并在任何你想要的地方使用它。:) 希望它能帮助你。 :)

+0

其实我正在用ngroute我猜$ stateParams是提供与ui路由器module.The问题在这里,当页面刷新查询是空的,所以我什么都得不到...感谢无论如何: ) – desmanda

相关问题