angular-promise

How do I use Bluebird with Angular?

筅森魡賤 提交于 2019-11-26 18:34:34
I tried using Angular with Bluebird promises: HTML: <body ng-app="HelloApp"> <div ng-controller="HomeController">{{name}} {{also}}</div> </body> JS: // javascript var app = angular.module('HelloApp', []); app.controller("HomeController", function ($scope) { var p = Promise.delay(1000).then(function () { $scope.name = "Bluebird!"; console.log("Here!", $scope.name); }).then(function () { $scope.also = "Promises"; }); $scope.name = "$q"; $scope.also = "promises"; }); window.app = app; [ Fiddle ] However, no matter what I tried, it kept staying "$q promises" and did not update. Except if I added a

Make angular.forEach wait for promise after going to next object

南笙酒味 提交于 2019-11-26 18:17:01
问题 I have a list of objects. The objects are passed to a deferred function. I want to call the function with the next object only after the previous call is resolved. Is there any way I can do this? angular.forEach(objects, function (object) { // wait for this to resolve and after that move to next object doSomething(object); }); 回答1: Before ES2017 and async/await (see below for an option in ES2017), you can't use .forEach() if you want to wait for a promise because promises are not blocking.

Does never resolved promise cause memory leak?

寵の児 提交于 2019-11-26 15:07:45
I have a Promise . I created it to cancel an AJAX request if needed. But since I don't need to cancel that AJAX, I've never resolved it and AJAX completed successfully. A simplified snippet: var defer = $q.defer(); $http({url: 'example.com/some/api', timeout: defer.promise}).success(function(data) { // do something }); // Never defer.resolve() because I don't need to cancel that ajax. What happens to this promise after request? Do never resolved promises like that cause memory leaks? Do you have any advice about how to manage Promise life cycle? Well, I'm assuming you don't keep an explicit

ES6 Promise not Updating AngularJS DOM [duplicate]

ぐ巨炮叔叔 提交于 2019-11-26 14:39:14
问题 This question already has an answer here: Can't get ES6 promise value to display in AngularJS view 1 answer I'm having trouble understanding angular components scope. If I do something like: function myComponent(){ this.data = 'Hello World'; } let myModule = angular.module('myModule', []); myModule.component('myComponent', { template: `<div>{{$ctrl.data}}</div>`, controller: myComponent }); <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs

Wait for all promises to resolve

☆樱花仙子☆ 提交于 2019-11-26 12:53:10
So I have a situation where I have multiple promise chains of an unknown length. I want some action to run when all the CHAINS have been processed. Is that even possible? Here is an example: app.controller('MainCtrl', function($scope, $q, $timeout) { var one = $q.defer(); var two = $q.defer(); var three = $q.defer(); var all = $q.all([one.promise, two.promise, three.promise]); all.then(allSuccess); function success(data) { console.log(data); return data + "Chained"; } function allSuccess(){ console.log("ALL PROMISES RESOLVED") } one.promise.then(success).then(success); two.promise.then(success

How can I access a variable outside a promise `.then` method?

感情迁移 提交于 2019-11-26 12:47:30
问题 I\'m working on a Spotify app. I\'m able to login and get my token. My problem is I cannot access a variable outside the method. In this case \"getCurrentUser\" This is my method: function getUser() { if ($localStorage.token == undefined) { throw alert(\"Not logged in\"); } else { Spotify.getCurrentUser().then(function(data) { var names = JSON.stringify(data.data.display_name); console.log(names) }) } }; As you can see I console.logged the name and I do get the right value in the console. But

How to use the axios library with AngularJS

大城市里の小女人 提交于 2019-11-26 11:38:58
问题 Why axios callback changes are displayed in angularjs, without using $apply I was trying axios library on angularjs and I was surprised when I saw that the changes to $scope in the axios callback were detected by angular. I thought I had to call $apply like, for example, when you use setTimeout . // axios example axios.get(url).then((response) => { // Here I don\'t need $apply, why?? $scope.axiosResult = response.data; }); // setTimeout example setTimeout(() => { // Here I need $apply for the

How do I sequentially chain promises with angularjs $q?

北战南征 提交于 2019-11-26 11:14:14
问题 In the promise library Q , you can do the following to sequentially chain promises: var items = [\'one\', \'two\', \'three\']; var chain = Q(); items.forEach(function (el) { chain = chain.then(foo(el)); }); return chain; however, the following doesn\'t work with $q : var items = [\'one\', \'two\', \'three\']; var chain = $q(); items.forEach(function (el) { chain = chain.then(foo(el)); }); return chain; 回答1: Redgeoff, your own answer is the way I used to translate an array into a chained

How to use $http promise response outside success handler

天大地大妈咪最大 提交于 2019-11-26 10:57:47
$scope.tempObject = {}; $http({ method: 'GET', url: '/myRestUrl' }).then(function successCallback(response) { $scope.tempObject = response console.log("Temp Object in successCallback ", $scope.tempObject); }, function errorCallback(response) { }); console.log("Temp Object outside $http ", $scope.tempObject); I am getting response in successCallback but not getting $scope.tempObject outside $http . its showing undefined . How to access response or $scope.tempObject after $http But if I want to use $scope.tempObject after callback so how can I use it. ? You need to chain from the httpPromise.

What happens with $q.all() when some calls work and others fail?

我的梦境 提交于 2019-11-26 10:56:29
问题 What happens with $q.all() when some calls work and others fail? I have the following code: var entityIdColumn = $scope.entityType.toLowerCase() + \'Id\'; var requests = $scope.grid.data .filter(function (rowData, i) { return !angular.equals(rowData, $scope.grid.backup[i]); }) .map(function (rowData, i) { var entityId = rowData[entityIdColumn]; return $http.put(\'/api/\' + $scope.entityType + \'/\' + entityId, rowData); }); $q.all(requests).then(function (allResponses) { //if all the requests