使用ng-include
时,pushState
功能被破坏。
下面是一个简单的例子。
<div ng-app="angularApp" ng-controller="angularCtrl">
<div ng-include="templateUrl"></div>
<button ng-click="bye()">BYE</button>
</div>
和
let app = angular.module('angularApp', []);
app.controller('angularCtrl', async function ($scope) {
$scope.templateUrl = '/assets/hi.html';
$scope.bye = function () {
$scope.templateUrl = '/assets/bye.html';
history.pushState({},'','/finish.html')
};
});
我们想在使用BYE
按下ng-include
按钮后更改正文值,并通过pushState
更改页地址。此示例是一个较大的项目的一部分,该项目在此处已尽可能简化。
注:根据评论,URL由pushState更改,但立即返回其值。在ng-include过程中将忽略它。
更改浏览器位置在AngularJS中本质上是棘手的。最好尝试尽可能多地使用$location
服务,因为这可以更好地理解$scope
摘要机制并与之配合使用。