javascript - History.popstate 不起作用,并且在浏览器中的前按钮和后按钮中调用

标签 javascript html angularjs html5-history web-frontend

每当我按下后退按钮时,我都会尝试获取之前的状态。在检查中,我可以看到 popstate 函数也被调用用于前进按钮。即使 popstate 函数被执行,它也不会返回到以前的状态。我无法理解如何正确协调事件,因为我是前端开发的新手。 提前致谢!!!

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="aid.js">    </script> 
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div id="one" ng-hide="val_1">{{val_1}} ONE </div>
<div id="two" ng-show="val_2">{{val_2}} TWO </div>
<button id="but" ng-click=myfun()> pressme </button>
</body>
</html>

援助.js

//the js code
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.val_2 = false;
    $scope.val_1 = false;
    $scope.myfun = function () {
        $scope.val_1 = true;
        $scope.val_2 = true;
        history.pushState("","","new");
    }
    window.addEventListener('popstate',function()
    {
    $scope.val_2 = false;
    $scope.val_1 = false;
    });
});

最佳答案

查看https://developer.mozilla.org/en-US/docs/Web/Events/popstate

前进和后退按钮都会调用 popstate。

关于javascript - History.popstate 不起作用,并且在浏览器中的前按钮和后按钮中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659454/

相关文章:

jquery - 扩展 javascript 库(underscore、jquery、backbone 等)

javascript - 更改数组而不链接引用

javascript - Safari 10 CSS.supports ('display' , 'contents' ) 返回 true 即使它不受支持?

html - translateX 覆盖 translateY

angularjs - 在 Angular 的另一个变量中使用变量?

angularjs - 如何在元素属性中使用 bind-once 或 angular-once

javascript - 链接 Promise 错误处理程序

javascript - 打包node-webkit App

javascript - 使用 Google 搜索 HTML

css - 将文本居中并将 div float 到右侧