为什么使用ng-include时pushState会中断?

问题描述 投票:0回答:1

使用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 pushstate angularjs-ng-include
1个回答
0
投票

更改浏览器位置在AngularJS中本质上是棘手的。最好尝试尽可能多地使用$location服务,因为这可以更好地理解$scope摘要机制并与之配合使用。

© www.soinside.com 2019 - 2024. All rights reserved.