如何调试角度路由?

时间:2015-09-27 18:12:12

标签: angularjs

我正在尝试构建一个简单的应用。

我的index.html文件

<!DOCTYPE html>
<html>
    <head>
        <script src="/bower_components/angular/angular.js"></script>
        <script src="/bower_components/angular-route/angular-route.js"></script>
        <script src="/js/main.js"></script>
        <script src="/js/controllers/account.js"></script>
    </head>
    <body ng-app="elara">
    wuuuu
    <div ng-view></div>

    </body>
</html>

main.js文件

var app = angular.module('elara', [
    'ngRoute'
]);

app.config(['$routeProvider',
    function($routeProvider) {
        debugger;
        $routeProvider.
            when('/', {
                templateUrl: '/html/template/home-page.html',
                controller: 'accountController'
            }).
            otherwise({
                redirectTo: '/phones'
            });
    }]);

和我的控制员:

var app = angular.module('elara', []);
app.controller('accountController', ['$scope', '$http', function(){
    $scope.username = "";
    $scope.password = "";

    $scope.register = function(){
        $http.post(config.apiAddress + "account/login/", {username: $scope.username, password: $scope.password}).then(
            function(data){
                console.log(data);
            }, function(response){
                console.log(response);
            });
    };
}]);

请在main.js文件中注明调试器,但是degugger在此时没有停止。它看起来不像在我的app.config函数中执行什么。当导航到/路线时,我的home-page.html未加载到ng-view

2 个答案:

答案 0 :(得分:0)

您可以使用$routeChangeError事件,该事件将在其他参数中提供rejection对象。

app.run(function ($rootScope) {
    $rootScope.$on('$routeChangeError', function (evt, current, previous, rejection) {
        console.log('Route error', rejection);
    });    
});

参考:$route docs

答案 1 :(得分:0)

您可以收听$ route服务发出的多个事件。这些事件是:

  1. $ routeChangeStart
  2. $ routeChangeSuccess
  3. $ routeChangeError
  4. $路由更新
  5. 您可以在此主题中找到更多信息 - https://stackoverflow.com/a/28016993/698127

相关问题