【发布时间】:2016-09-20 07:07:28
【问题描述】:
我正在使用 Angular 来渲染这样的视图:
var app = angular.module("demo", []);
app.controller('demoCtrl', ["$scope",function ($scope) {
$scope.current_step = 1;
$scope.step_two = function(){
$scope.current_step = 2;
};
$scope.step_one = function(){
$scope.current_step = 1;
};
}]);
.button {
background: #fb6648;
color: #fff;
display: inline-block;
padding: 18px 0px;
max-width: 150px;
width: 100%;
text-align: center;
border-radius: 3px;
font-size: 18px;
transition: all 0.5s;
outline: none;
border: none;
-webkit-appearance: none!important;
}
.area {
width: 100px;
height: 100px;
display: block;
border: 1px solid;
text-align: center;
margin-bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="demo">
<div ng-controller="demoCtrl" id="demoCtrl">
<div ng-if="current_step==1">
<span class="area" >Step One</span>
<a class="button" ng-click="step_two()" >go to Step Two</a>
</div>
<div ng-if="current_step==2">
<span class="area">Step Two</span>
<a class="button" ng-click="step_one()" >Back to Step One</a>
</div>
</div>
</body>
我希望此按钮在按下浏览器后退和前进按钮时工作。 我尝试更改哈希,但没有成功。
【问题讨论】:
-
你需要使用角度路由器
-
是的,我知道,但我已经构建了没有角路由器的项目。不想为这个小功能包含它。
-
后退或前进时页面丢失
-
@azad 没有任何办法,所以我可以更改 url 中的哈希或操纵浏览器历史记录,以便浏览器恢复以前的状态或至少触发一个事件,以便我可以自己恢复范围。
-
@GauravSrivastava 如果你想操纵浏览器历史的状态,使用可以看这个github.com/browserstate/history.jsjohan.driessen.se/posts/…
标签: angularjs