angular-promise

resolve multiple promises in angularjs

大兔子大兔子 提交于 2019-12-24 00:25:24
问题 Here is my factory which gets data from an API and stores each data to a SQLLite database. team.factory('dataSync', function($q,$http,$timeout,$cordovaSQLite ){ return { getData:function(){ var q = $q.defer(); $http.get(api+'/sync/').then(function(response){ q.resolve(response); },function(error){ q.reject(); }) return q.promise; }, saveData:function(){ var q= $q.defer(); this.getData().then(function(result){ var data= result.data; var sharing = data.sharing; var help = data.help; var message

AngularJS socket.io event forwarding with deferred promise

纵然是瞬间 提交于 2019-12-23 22:18:33
问题 I am using angular-socket-io but needed a way to authenticate in the app first before starting the socket.io interface. I followed this method. But I need to use event forwarding in the factory as shown here like this: socket.forward("event"); How can I add forwarding in my factory? .factory('socket', function($rootScope, $timeout, socketFactory, $q, $log) { // create a promise instance var socket = $q.defer(); // listen for the authenticated event emitted on the rootScope of // the Angular

Bind ng-repeat to a promise

女生的网名这么多〃 提交于 2019-12-23 20:16:16
问题 I'm trying to bind a $resource promise to ng-repeat. But I can't make it work. The HTML <ul ng-controller="ColorsCtrl"> <li ng-repeat="colors in getColors()"></li> </ul> The Script app.module("myApp", ['ngResource']) .factory('Color', function($resource) { return $resource('/colors.json'); }) .controller('ColorsCtrl', function($scope, Color) { $scope.getColors = function() { return Color.query(); } }); 回答1: You don't need to bind your UI to a promise: you bind the UI to an array, then you

Resolving promises without a digest cycle

心已入冬 提交于 2019-12-23 20:12:50
问题 AngularJS promises seem to be tied to a digest cycle, as in, the success/error callbacks are not called until a digest cycle is run. This means that anything that uses promises, such as $http or manually created promises, also need to trigger a digest cycle in order to get the callbacks to run. Is it possible to use promises in Angular, without the digest cycle being run at all? I realise you can use $applyAsync , which schedules the digest cycle for a bit later, but I'm looking to not run

Break Out of then promises in Angularjs

孤街醉人 提交于 2019-12-23 20:04:09
问题 I am trying to find a way to break out of a promise chain in AngularJS code. The obvious way was to return an object and then check is validity in every "then" function in the chain. I would like to find a more elegant way of breaking out of a then chain. 回答1: In angular, there is the $q service that can be injected in directives, controllers etc, that is a close implentation of Kris Kowal's Q. So inside of then function instead of returning a value or something else that would be chained to

Break out of promise block in a for loop

半城伤御伤魂 提交于 2019-12-23 13:21:07
问题 I have the following code - this.storeNameValidate = function(stores) { var deferred = $q.defer(); console.log(stores); for (storeIndex in stores) { this.nameValidate(stores[storeIndex].storeName, 3, 10) .then(function() { console.log("valid store name"); }, function() { console.log("invalid store name"); deferred.reject("invalid store name"); }) } return deferred.promise; } I need to break out off the for loop if the nameValidate error block is called. How can i do this ? I can do it

Angular $q.all gets called after first promise finished

末鹿安然 提交于 2019-12-23 12:03:19
问题 I'm trying to use $q.all to wait until all promises are resolved but it's called after first promise is finished! What I'm doing wrong? function sendAudits(audits) { var promises = []; $scope.sendAudits = { progress: 0 }; angular.forEach(audits, function (audit, idAudit) { promises.push(saveAudit(audit)); }); $q .all(promises) .then(function (data) { console.log(data); }, function (errors) { console.log(errors); }); } function saveAudit(audit) { var filename = audit.header.id + ".txt"; return

issue mixing promises with HttpInterceptor observables?

落花浮王杯 提交于 2019-12-23 05:47:08
问题 I am using the HttpInterceptor to resend requests with a token in case they return a 401. This had worked well before, when I was just taking the cached token. Since the Firebase token does not seem to be automatically refreshed (using forceRefresh though), I am now trying to get a fresh token in realtime in the interceptor class. The problem is that now the request is not being re-send. Here is my full interceptor: export class CustomHttpInterceptor implements HttpInterceptor { constructor

Using $timeout in service: this.func is not a function

人盡茶涼 提交于 2019-12-23 04:48:32
问题 I've been trying to use promise to end user session after certain amount of time. Problem is, whenever a function defined in service is called from the function triggered by $timeout, the function seems to be undefined. I think it's some kind of a scope issue, but I have not managed to fix this on my own. app.service('sessionService', function($timeout) { var closeSession = function() { this.resetUserInfo() // maybe do other things as well } this.start = function() { console.log("start")

Component : ui-router resolve will not inject into controller, data undefined

筅森魡賤 提交于 2019-12-23 01:42:10
问题 I have this problem using resolve with component & ui-router, the data "after" resolving promise are "undefined" in a controller Service: class userService { constructor ($http, ConfigService, authService) { this.$http = $http; this.API_URL = `${ConfigService.apiBase}`; this.authService = authService; } testAuth () { return this.$http.get(this.API_URL + '/test-auth') } getCollaboratores () { return this.$http.get(this.API_URL + '/collaboratores').then( (resolve) => { // promise resolve