我正在关注AngularJS网站上的教程,我遇到了麻烦。我试图使用console.log()和警报自己调试问题。似乎http请求的某些内容导致它在无限循环中调用ngRoute指令。
我已经阅读了一些其他帖子,说这是因为服务器返回index.html作为对http请求的响应。我认为这是我的问题,但我不确定如何检查从我的GET请求返回的内容。
如何测试返回的内容并阻止此无限循环?
main.js内容
var coffeeBeanApp = angular.module('coffeeBeanApp', [
'ngRoute',
'coffeeBeanControllers'
]);
coffeeBeanApp.config(['$routeProvider', function($routeProvider)
{
$routeProvider.
when('/coffee', {
templateUrl: 'partials/coffee-list.html',
controller: 'CoffeeListCtrl'
}).
when('/coffee/:slug', {
templateUrl: 'partials/coffee-detail.html',
controller: 'CoffeeDetailCtrl'
}).
otherwise({
redirectTo: '/coffee'
});
}]);
controllers.js
var coffeeBeanControllers = angular.module('coffeeBeanControllers', []);
coffeeBeanControllers.controller('CoffeeListCtrl', function ($scope, $http) {
$http.get('/api/coffee').success(function(data){$scope.beans = data;})
});
coffeeBeanControllers.controller('CoffeeDetailCtrl',
['$scope', '$routeParams', function($scope, $routeParams){
$scope.slug = $routeParams.slug;
}]);
答案 0 :(得分:0)
尝试此操作以获得更好的调试:
$http.get('/api/coffee').then((function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response);
});
或者您可以在成功回调中添加console.log(在https://docs.angularjs.org/api/ng/service/ $ http中将其标记为红色已弃用
$http.get('/api/coffee').success(function(data){console.log(data);});