自定义 Angular 服务出错:无法读取 属性
Error in Custom Angular Service: Cannot read property
下面是plnkr link。我创建了一个自定义 Angular 服务,但它不工作。我得到的错误在这里:
TypeError: Cannot read property 'then' of undefined
这里有一个演示:http://plnkr.co/edit/Jae0nymkg6wyZYPBvmao?p=preview
我的代码如下:
var app = angular.module('myApp', []);
app.controller('mainCtrl', function(
$scope, github, $interval,
$log, $anchorScroll, $location) {
$scope.message = "hi";
var usercomplete = function(data) {
$scope.user = data;
github.getRepos($scope.user).then(Repository, onerror);
};
var Repository = function(data) {
$scope.repos = data;
$location.hash("userDetails");
$anchorScroll();
};
var onerror = function(reason) {
$scope.error = 'Could not fetch the data';
};
var decrement = function() {
$scope.countdown -= 1;
if ($scope.countdown < 1) {
$scope.search($scope.username);
}
};
var countDownInterval = null;
var startCountDown = function() {
countDownInterval = $interval(decrement, 1000, $scope.countdown);
};
$scope.search = function(username) {
github.getUser(username).then(usercomplete, onerror);
if (countDownInterval) {
$interval.cancel(countDownInterval);
$scope.countdown = null;
}
};
$scope.username = 'Angular';
$scope.repoSortOrder = "-stargazer_count";
$scope.countdown = 5;
startCountDown();
});
谢谢。
在服务中使用这种方法。已经在使用中了。再次使用控制器..所以可能会出现问题
var getUser = function(username) {
return $http.get('https://api.github.com/users/' + username)
.success(function (response) {
return response.data;
}).error(function (err) {
return err;
});
};
下面是plnkr link。我创建了一个自定义 Angular 服务,但它不工作。我得到的错误在这里:
TypeError: Cannot read property 'then' of undefined
这里有一个演示:http://plnkr.co/edit/Jae0nymkg6wyZYPBvmao?p=preview
我的代码如下:
var app = angular.module('myApp', []);
app.controller('mainCtrl', function(
$scope, github, $interval,
$log, $anchorScroll, $location) {
$scope.message = "hi";
var usercomplete = function(data) {
$scope.user = data;
github.getRepos($scope.user).then(Repository, onerror);
};
var Repository = function(data) {
$scope.repos = data;
$location.hash("userDetails");
$anchorScroll();
};
var onerror = function(reason) {
$scope.error = 'Could not fetch the data';
};
var decrement = function() {
$scope.countdown -= 1;
if ($scope.countdown < 1) {
$scope.search($scope.username);
}
};
var countDownInterval = null;
var startCountDown = function() {
countDownInterval = $interval(decrement, 1000, $scope.countdown);
};
$scope.search = function(username) {
github.getUser(username).then(usercomplete, onerror);
if (countDownInterval) {
$interval.cancel(countDownInterval);
$scope.countdown = null;
}
};
$scope.username = 'Angular';
$scope.repoSortOrder = "-stargazer_count";
$scope.countdown = 5;
startCountDown();
});
谢谢。
在服务中使用这种方法。已经在使用中了。再次使用控制器..所以可能会出现问题
var getUser = function(username) {
return $http.get('https://api.github.com/users/' + username)
.success(function (response) {
return response.data;
}).error(function (err) {
return err;
});
};