Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载视图时angularjs清除历史记录_Angularjs - Fatal编程技术网

加载视图时angularjs清除历史记录

加载视图时angularjs清除历史记录,angularjs,Angularjs,当通过routeProvider加载特定视图时,是否有方法清除Angular JS存储的历史记录 我正在使用angular创建一个公共安装,历史记录将积累很多,因此我希望在加载主页时清除它。在控制器中,我添加了一个函数来更改视图: $scope.changeView = function(view) { $location.path(view); $location.replace(); } 然后在视图中,我添加了一个ng click来调用此函数: <div ng-cli

当通过
routeProvider
加载特定视图时,是否有方法清除Angular JS存储的历史记录


我正在使用angular创建一个公共安装,历史记录将积累很多,因此我希望在加载主页时清除它。

在控制器中,我添加了一个函数来更改视图:

$scope.changeView = function(view) {
    $location.path(view);
    $location.replace();
}
然后在视图中,我添加了一个ng click来调用此函数:

<div ng-click="changeView('/app/new_view')">

方法用于替换历史堆栈中最顶层的视图,而不是添加新视图

有一种特殊的
replace
方法,可用于告诉$location服务下次将$location服务与浏览器同步时,应替换上一条历史记录,而不是创建新的历史记录

参考:
控制器中的$location注入$scope和$location

$scope.$on('$viewContentLoaded', function(){
  //view loaded do some stuff.
    $location.replace(); //clear last history route
});
for(var a=0;a<$rootScope.$viewshistory.histories.root.stack.length;a++){
console.log(“对于历史记录删除”+a);
删除$rootScope.$viewshistory.histories.root.stack[a];
}

这不是该代码的功能。相反,它加载新视图并将其添加到历史记录中,替换历史记录中的最后一个元素。请参阅
$rootScope。$viewshistory
未定义。它是您正在添加到
$rootScope
或提供它的东西。
 for(var a=0; a < $rootScope.$viewHistory.histories.root.stack.length; a++ ){
         console.log(" for history remove " + a );
         delete $rootScope.$viewHistory.histories.root.stack[a];
     }
var dummyState = {app: 'resto'};

// set up history state
if (!$window.history.state) {
  $window.history.pushState(dummyState, '');
}

// when history back
$window.onpopstate = function() {
  console.log('window.onpopstate:url=', $window.location.href);
  console.log('window.onpopstate:history.state=', $window.history.state);

  // repopulate history state so we get the popstate event again
  $window.history.pushState(dummyState, '');
};