2015-09-27 113 views
0

我正在嘗試構建一個簡單的應用程序。如何調試角路由?

我的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); 
      }); 
    }; 
}]); 

請noitice在main.js文件中的調試器,但degugger目前還沒有停止。它看起來不像執行我的app.config函數中的內容。當瀏覽到/路線,我家page.html中未裝入ng-view

+2

順便說一句,你忘了參數'$ scope'和'$ http'給控制器。 –

回答

0

您可以使用$routeChangeError事件,這將提供其他參數之間的rejection對象。

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

參考:$route docs

+0

將此代碼塊添加到main.js文件的底部,但沒有記錄到控制檯:) – Sefa

+0

好像好像我誤解了它。我從不使用'ngRoute'總是使用'ui-router',它的等效錯誤方法確實是以這種方式觸發的。似乎在'ngRoute'只是爲了解決 – charlietfl