angular-promise

How to handle error in angular-ui-router's resolve

二次信任 提交于 2019-11-28 18:33:59
I am using angular-ui-router's resolve to get data from server before moving to a state. Sometimes the request to the server fails and I need to inform the user about the failure. If I call the server from the controller, I can put then and call my notification service in it in case the call fails. I put the call to the server in resolve because I want descendant states to wait for the result from the server before they start. Where can I catch the error in case the call to the server fails? (I have read the documentation but still unsure how. Also, I'm looking for a reason to try out this new

Error handling in AngularJS http get then construct

北城以北 提交于 2019-11-28 18:31:16
How can I handle an HTTP error, e.g. 500, when using the AngularJS "http get then" construct (promises)? $http.get(url).then( function(response) { console.log('get',response) } ) Problem is, for any non 200 HTTP response, the inner function is not called. You need to add an additional parameter: $http.get(url).then( function(response) { console.log('get',response) }, function(data) { // Handle error here }) You can make this bit more cleaner by using: $http.get(url) .then(function (response) { console.log('get',response) }) .catch(function (data) { // Handle error here }); Similar to @this.lau

ControllerProvider in UI-router results in error

六月ゝ 毕业季﹏ 提交于 2019-11-28 10:44:54
问题 I have a ui-router StateProvider and need to pick between controllers & views based on external data, so I used TemplateProvider and ControllerProvider . If I only had the TemplateProvider it all works fine, but when I add the ControllerProvider I get this error: Error: [ng:areq] Argument 'fn' is not a function, got Object http://errors.angularjs.org/1.3.1/ng/areq?p0=fn&p1=not%20aNaNunction%2C%20got%Object at REGEX_STRING_REGEXP (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:80

AngularJS promise

≡放荡痞女 提交于 2019-11-28 10:03:26
AngularJS docs say : $q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values. So could someone please explain the reason this fiddle not working? It's not possible to change text field value. But assigning promises that $http service returns to a scope field works like a charm. Controller: function MyController($scope, $q, $timeout) { this.getItem = function () { var deferred = $q.defer(); deferred.resolve({ title: 'Some title' }); return deferred.promise; }; $scope.item = this

Immediately return a resolved promise using AngularJS

烈酒焚心 提交于 2019-11-28 09:39:47
I'm trying to get my head around promises in JavaScript (in particular AngularJS). I have a function in a service, let's call it fooService , that checks if we've loaded some data. If it has, I just want it to return, and if we haven't, we need to load the data and return a promise: this.update = function(data_loaded) { if (data_loaded) return; // We've loaded the data, no need to update var promise = Restangular.all('someBase').customGet('foo/bar').then(function(data) { // Do something with the data here } return promise; } I have another function that then calls the update function of

javascript, promises, how to access variable this inside a then scope [duplicate]

本秂侑毒 提交于 2019-11-28 07:13:15
This question already has an answer here: Object method with ES6 / Bluebird promises 2 answers I want to be able to call a function inside the .then scope, and for that I use the this.foo() manner. But if I do this inside the .then I get an error, since this appears to be lost. What can I do? In this code, this would be equivalent to have the same output for the object this console.log(this) one().then(function() { console.log(this) }) function one() { var deferred = $q.defer(); deferred.resolve() return deferred.promise; } This neither seems to work console.log(this) var a = this; one().then

Is there a shortcut to proxy-resolve/reject a promise to an Angular $q deferred?

旧巷老猫 提交于 2019-11-28 05:35:46
问题 Given an unresolved deferred ( dfd ), and a then-able promise ( promise ), which may or may not be deferred, is there a way to 'proxy' the promise into the deferred? The semantics should be as so: promise.then(dfd.resolve, dfd.reject); The $q documentation only mentions handling of rejected promises (and furthermore, only promises rejected in a certain way): defered.resolve(value) – resolves the derived promise with the value. If the value is a rejection constructed via $q.reject, the promise

$q promise with Underscore _each

◇◆丶佛笑我妖孽 提交于 2019-11-28 02:01:49
So I have a method in a angularjs server that is calling a method that returns a promise for each method in a array. I am using underscore _each to loop through the array. I want to wait until the entire array is processed before I call the final line of code in the method.. So... function ProcessCoolStuff(coolStuffs) { var stuff = []; _.each(coolStuffs, function(coolStuff) { //Some method using $q to return makeStuffCooler(coolStuff).then(function(coolerStuff) { stuff.push(coolerStuff); }); }); //Maybe Call a Display Method, or call event ect.. ShowAllMyCoolStuff(stuff); } This of course does

AngularJS $http call in a Service, return resolved data, not promises

可紊 提交于 2019-11-27 22:18:12
I want to know if it is possible to make a service call that uses $http so it returns data directly without returning a promise? I have tried to use the $q and defer without any luck. Here is what I mean: I have a service: angular.module('myModule').factor('myService', ['$http', '$q', function($http, $q) { // Public API return { myServiceCall: function() { return $http.get('/server/call'); } }; } ]); And this is how I would call it: // My controller: myService.myServiceCall().then(function(data) { $scope.data = data; }); I want to avoid that and instead wish to have: $scope.data = myService

AngularJS - Promises rethrow caught exceptions

瘦欲@ 提交于 2019-11-27 21:19:59
In the following code, an exception is caught by the catch function of the $q promise: // Fiddle - http://jsfiddle.net/EFpn8/6/ f1().then(function(data) { console.log("success 1: "+data) return f2(); }) .then(function(data) {console.log("success 2: "+data)}) .catch(function(data) {console.log("error: "+data)}); function f1() { var deferred = $q.defer(); // An exception thrown here is not caught in catch // throw "err"; deferred.resolve("done f1"); return deferred.promise; } function f2() { var deferred = $q.defer(); // An exception thrown here is handled properly throw "err"; deferred.resolve(