我的控制器注入了所有必需的依赖项。
$scope.connect = function(url) {
var defer = $q.defer();
var promise = $http.get(url).then(function (response) {
$timeout(function(){
defer.resolve(response);
},10000);
defer.resolve(response);
$scope.$apply(); //$rootScope.$apply();
});
return defer.promise;
};
$scope.mymethod = function(){
$scope.globalMydata[];
console.log("before the http get");
$scope.connect("MY_URL").then(function(data) {
console.log("called!", data);
console.log("INSIDE the http get");
$scope.mydata = data;
//$scope.globalMydata.push(data);
});
console.log("after the http get ");
//do some processing of the returned data here
var dataHolder = $scope.mydata;
return calculatedValue;//value from procesing
}
当代码被执行时,“INSIDE the http get”作为最后的调试日志被调用。我从 get 调用中得到结果,但由于它稍后返回,我无法对其进行任何处理。这就是我们承诺的确切原因吗?我需要承诺的数据在控制器内部进行一些处理。
我的承诺实施中有任何问题吗?