i have problems when it comes to $http promises in angularjs. i am doing this in my service: (the getSomething function should chain two promises)
当谈到angularjs中的$ http承诺时,我遇到了问题。我在我的服务中这样做:( getSomething函数应链接两个promise)
the second function uses a external callback function!
第二个函数使用外部回调函数!
app.service('blubb', function($http, $q) { var self = this; this.getSomething = function(uri, data) { return self.getData(uri).then(function(data2) { return self.compactData(uri, data2); }); }; this.getData = function(uri) { var deferred = $q.defer(); $http.get(uri).success(function(data) { deferred.resolve(data); }).error(function() { deferred.reject(); }); return deferred.promise; }; this.compactData = function(uri, data) { var deferred = $q.defer(); /* callback function */ if(!err) { console.log(compacted); deferred.resolve(compacted); } else { console.log(err); deferred.reject(err); } /* end of function */ return deferred.promise; }; });
when i use the service in my controller it doesn't output the console.log:
当我在我的控制器中使用该服务时,它不会输出console.log:
blubb.getSomething(uri, input).then(function(data) { console.log(data) });
edit: if i define the callback function by myself in 'compactData' it works, but i am using "jsonld.compact" from https://raw.github.com/digitalbazaar/jsonld.js/master/js/jsonld.js and THIS doesn't work!
编辑:如果我自己在'compactData'中定义回调函数它可以工作,但我使用https://raw.github.com/digitalbazaar/jsonld.js/master/js/jsonld.js中的“jsonld.compact”这不起作用!
jsonld.compact(input, context, function(err, compacted) { if(!err) { console.log(compacted); deferred.resolve(compacted); } else { deferred.reject('JSON-LD compacting'); } });
i am getting the console.log output in jsonld.compact but the resolve doesn't work and i don't know why..
我在jsonld.compact中获取console.log输出,但解决方案不起作用,我不知道为什么..
it only works with $rootScope.$apply(deferred.resolve(compacted));
它只适用于$ rootScope。$ apply(deferred.resolve(compacted));
4 个解决方案
#1
5
I'm using chaining promises like this:
我正在使用这样的链接承诺:
$http.get('urlToGo')
.then(function(result1) {
console.log(result1.data);
return $http.get('urlToGo');
}).then(function(result2) {
console.log(result2.data);
return $http.get('urlToGo');
}).then(function(result3) {
console.log(result3.data);
});
#2
1
Chaining promises works here : jsfiddle
链接承诺在这里工作:jsfiddle
In your implementation, if $http.get
or compactData
goes wrong your console.log(data)
will not be call.
在您的实现中,如果$ http.get或compactData出错,则不会调用console.log(data)。
You should maybe catch errors :
您应该捕获错误:
blubb.getSomething(uri, input).then(function(data) {
console.log(data);
}, function(err) {
console.log("err: " + err);
});
#3
1
Whenever you use an external (external to AngularJS) callback that runs in a new turn/tick, you have to call $apply() on the appropriate scope after it has been invoked. This lets AngularJS know it has to update. You'll probably want to make sure you're only calling it once -- after all of the promises have been resolved. As an aside, jsonld.js provides a promises/future API, so if you're already using promises, you don't have to do that wrapper code above. Instead you can do:
每当你使用在新的转弯/勾号中运行的外部(AngularJS外部)回调时,你必须在调用后在适当的范围内调用$ apply()。这让AngularJS知道它必须更新。在所有承诺都得到解决之后,您可能希望确保只调用一次。顺便说一下,jsonld.js提供了promises / future API,所以如果你已经在使用promises,那么你不必在上面做那个包装器代码。相反,你可以这样做:
var promisesApi = jsonld.promises();
var promise = promisesApi.compact(input, context);
// do something with the promise
#4
0
I would suggest you to use a Factory instead of a service.
我建议你使用Factory而不是服务。
Just return the function from the factory and use it in your controller
只需从工厂返回功能并在控制器中使用它
#1
5
I'm using chaining promises like this:
我正在使用这样的链接承诺:
$http.get('urlToGo')
.then(function(result1) {
console.log(result1.data);
return $http.get('urlToGo');
}).then(function(result2) {
console.log(result2.data);
return $http.get('urlToGo');
}).then(function(result3) {
console.log(result3.data);
});
#2
1
Chaining promises works here : jsfiddle
链接承诺在这里工作:jsfiddle
In your implementation, if $http.get
or compactData
goes wrong your console.log(data)
will not be call.
在您的实现中,如果$ http.get或compactData出错,则不会调用console.log(data)。
You should maybe catch errors :
您应该捕获错误:
blubb.getSomething(uri, input).then(function(data) {
console.log(data);
}, function(err) {
console.log("err: " + err);
});
#3
1
Whenever you use an external (external to AngularJS) callback that runs in a new turn/tick, you have to call $apply() on the appropriate scope after it has been invoked. This lets AngularJS know it has to update. You'll probably want to make sure you're only calling it once -- after all of the promises have been resolved. As an aside, jsonld.js provides a promises/future API, so if you're already using promises, you don't have to do that wrapper code above. Instead you can do:
每当你使用在新的转弯/勾号中运行的外部(AngularJS外部)回调时,你必须在调用后在适当的范围内调用$ apply()。这让AngularJS知道它必须更新。在所有承诺都得到解决之后,您可能希望确保只调用一次。顺便说一下,jsonld.js提供了promises / future API,所以如果你已经在使用promises,那么你不必在上面做那个包装器代码。相反,你可以这样做:
var promisesApi = jsonld.promises();
var promise = promisesApi.compact(input, context);
// do something with the promise
#4
0
I would suggest you to use a Factory instead of a service.
我建议你使用Factory而不是服务。
Just return the function from the factory and use it in your controller
只需从工厂返回功能并在控制器中使用它