使用后退按钮后,ngRoute不断回退到.otherwise路线

时间:2017-03-27 21:06:27

标签: angularjs ngroute angularjs-ng-route

我有2个页面:/teams和团队编辑页面/teamedit/:1,这里是路线:

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    var viewBase = '/views/';

    $routeProvider
        .when('/teams', {
            controller: 'teamsController',
            templateUrl: viewBase + 'teams/teams.html',
            controllerAs: 'vm'
        })
        .when('/teamedit/:id', {
            controller: 'teamEditController',
            templateUrl: viewBase + 'teams/teamEdit.html',
            controllerAs: 'vm'
        })
        .otherwise({ redirectTo: '/agents' });

    $locationProvider.html5Mode(true);
}]);

以下是导致我出现问题的步骤:

  1. /teams页面点击按钮打开/teamedit/1页面。对于即时通讯在控制器中使用$location.path(url + team);
  2. /teamedit/1已加载,我点击浏览器后退按钮
  3. 现在回到/teams页面,我再次点击按钮,我应该把我带到/teamedit/1页面,但它所做的就是加载那个bage一秒钟(控制器代码,所有内容都被执行)但是然后重定向回到.otherwise中设置的页面。

    没有理由回到otherwise,我该如何解决这个问题?

    编辑:添加代码。 按钮打开/ teamedit页面:

    <button class="btn btn-primary" ng-click="vm.edit('/teamedit/', team.teamId)"> <i class="glyphicon glyphicon-edit"></i> Edit</button>
    

    teamsController:

    (function () {
        'use strict';
    
        angular
            .module('actionSyncAdminApp')
            .controller('teamsController', teamsController);
    
        teamsController.$inject = ['$scope', '$location', 'teamsService', 'broadcaster', 'ngProgressFactory'];
    
        function teamsController($scope, $location, teamsService, broadcaster, ngProgressFactory) {
            $scope.title = 'teamsController';
    
            var vm = this;
    
            vm.progressbar = ngProgressFactory.createInstance();
            vm.progressbar.setParent(document.getElementById('sub-page'));
    
            vm.teams = [];
    
            vm.new = function (url) {
                $location.path(url);
            };
            vm.edit = function (url, team) {
                console.log(url + team);
                $location.path(url + team);
            };
    
            activate();
    
            function activate() {
                vm.progressbar.start();
                teamsService.getTeams().then(function (teams) {
                    vm.progressbar.complete();
                    vm.teams = teams;
                });
            }
        }
    })();
    

1 个答案:

答案 0 :(得分:0)

我发现了问题。这对我来说只是一个愚蠢的错误。我在button内有<a href ></a>,如下所示:

<a href tabindex="0" class="accordion-toggle" ng-click="toggleOpen()" uib-accordion-transclude="heading">
    <span uib-accordion-header ng-class="{'text-muted': isDisabled}">
         <button type="button" class="btn btn-primary" ng-click="vm.edit('/teamedit/', team.teamId)"> <i class="glyphicon glyphicon-edit"></i> Edit</button>
    </span>
</a>

奇怪的是,这只是在我使用了后退按钮之后打破了按钮,而不是在页面加载后第一次。无论如何现在都很好:))