的javascript
var app = angular.module('angularjsapp', []);
app.config(function($routeProvider){
$routeProvider.when('/page/:id', {templateUrl: 'Main.html' , controller: 'MainCtrl'});
$routeProvider.otherwise({templateUrl: 'Main.html' , controller: 'MainCtrl'});
});
app.controller('MainCtrl', function($scope, $routeParams) {
$scope.id = $routeParams.id || 'no id';
});
的index.html
<body>
<h1>Default route demo</h1>
<div ng-view></div>
<hr/>
<ul>
<li><a href="#/other">other</a></li>
<li><a href="#/page/1">Page 1</a></li>
<li><a href="#/page/2">Page 2</a></li>
<li><a href="#/page/3">Page 3</a></li>
<li><a href="#/page/4">Page 4</a></li>
</ul>
</body>
在AngularJS中,我知道我可以通过此代码获取我的url变量(在本例中为id
)。但是,一旦我刷新浏览器,变量就变成默认值(在这种情况下为nothing
)。
所以我的问题是我如何保留这个变量,以便我可以保持它甚至刷新浏览器。
答案 0 :(得分:1)
您可以使用https://github.com/grevory/angular-local-storage中的angular-local-storage模块来帮助您在刷新浏览器时保持状态。
以下是具有本地存储支持的分叉Anuglar演示。您必须添加自己的逻辑,以了解如何处理重新路由或使用上一页id