Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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(@R_119_10584@地)更改为http:// localhost / my / path / fruits / page / 2,然后恢复为http:// localhost / my / path / fruits /.

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

另外:如果我设置$LOCATIOnProvider.@L_502_0@mode(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.@R_375_10586@l_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 {{@R_375_10586@l_pages}} <a ng-click="nextPage()" ng-show="current_page < @R_375_10586@l_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>
我有同样的问题,不能为我的生活弄清楚出了什么问题. 对我有用的是更改锚@L_262_16@,该@L_262_16@将ng-click事件附加到按钮@L_262_16@上.从我可以收集的内容中,当没有给出href属性来阻止认操作时,Angular会修改标记的行为,因此如果您在Ng-click事件处理程序中手动更新位置,这可能会产生干扰.这只是我的猜测,所以希望有人能够更好地解释.

大佬总结

以上是大佬教程为你收集整理的routing – AngularJS location.path()简要更改URL,然后恢复全部内容,希望文章能够帮你解决routing – AngularJS location.path()简要更改URL,然后恢复所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。