routing – AngularJS location.path()简要更改URL,然后恢复

我有一个令人沮丧的早晨.

我有一个具有基本位置的应用程序,因此在头部是< base href =“/ my / path / fruits / index.html”/>.我正在尝试通过函数nextPage()设置分页.当我点击触发nextPage()的链接时,我使用$location.path(‘/ page /:page’).

预期行为:浏览器位置网址应更改为http:// localhost / my / path / fruits / page / 2.

实际行为:浏览器URL(非常简短地)更改为http:// localhost / my / path / fruits / page / 2,然后恢复为http:// localhost / my / path / fruits /.

注意:如果我没有使用基本位置,则似乎不会发生此行为.但是,我需要使用基本位置,因为应用程序位于服务器的子/子/子目录中.

另外:如果我设置$locationProvider.html5Mode(false),则不会发生此行为.

难道我做错了什么?这是Angular中的错误吗?任何帮助是极大的赞赏.

这是适当的文件.

index.html的:

<!doctype html>
<html ng-app="fruits">
  <head>
    <base href="/my/path/fruits/index.html" />
    <script src="angular.js"></script>
    <script src="script.js"></script>
  </head>
  <body>
    <div ng-controller="MainCntl">
      <div ng-view></div>
    </div>
  </body>
</html>

的script.js:

angular.module('fruits',[],function($routeProvider,$locationProvider) {
    $routeProvider
    .when('/',{
        templateUrl: 'fruits.html',controller: FruitCntl,})
    .when('/page/:page',})
    .otherwise({redirectTo:'/'})
    ;

    // configure html5 
    $locationProvider.html5Mode(true).hashPrefix('!');
}).filter('startFrom',function() 
    {
        return function(input,start) 
        {
            start = +start; //parse to int
            if (input == undefined){
                input = [];
            }
            return input.slice(start);
        }
})
;

function MainCntl($scope,$location){
    angular.extend($scope,{
        current_page: 1,per_page: 3,fruits: [
            {name: 'Apples',color: 'red'},{name: 'Bananas',color: 'yellow'},{name: 'Oranges',color: 'orange'},{name: 'Grapes',color: 'purple'},{name: 'Blueberries',color: 'blue'},{name: 'Strawberries',{name: 'Raspberries',{name: 'Clementines',{name: 'Pears',color: 'green'},{name: 'Mangoes',color: 'orange'}
        ],nextPage: function(){
            this.current_page++;
            $location.path('/page/'+this.current_page);
        },prevIoUsPage: function(){
            this.current_page--;
            $location.path('/page/'+this.current_page);
        }
    })
    $scope.total_pages = Math.ceil($scope.fruits.length / $scope.per_page);
}

function FruitCntl($scope,$location,$routeParams){
    if ($routeParams.page != undefined){
        $scope.current_page = $routeParams.page;
    }
}

fruits.html(视图)

Page: <a ng-click="prevIoUsPage()" ng-show="current_page > 1">PrevIoUs</a> {{current_page}} of {{total_pages}} <a ng-click="nextPage()" ng-show="current_page < total_pages">Next</a>
<div ng-repeat="fruit in fruits | startFrom:(current_page - 1)*per_page | limitTo:per_page">
    {{fruit.name}} are {{fruit.color}}<br/>
</div>
我有同样的问题,不能为我的生活弄清楚出了什么问题. 对我有用的是更改锚标签,该标签将ng-click事件附加到按钮标签上.从我可以收集的内容中,当没有给出href属性来阻止认操作时,Angular会修改标记的行为,因此如果您在ng-click事件处理程序中手动更新位置,这可能会产生干扰.这只是我的猜测,所以希望有人能够更好地解释.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...