angular-promise

AngularJS: How to get $http result as returned value of a function?

匆匆过客 提交于 2019-12-06 08:09:14
Inside my controller I need a function that takes a parameter and returned a URL from server. Something like this: $scope.getPoster = function(title) { return $http.get("http://www.omdbapi.com/?t=" + title + "&y=&plot=short&r=json"); }; And inside my view I need to pass the title this way and get result as src for ng-src : <div class="col-sm-6 col-md-3" ng-repeat="movie in movies"> <div class="thumbnail"> <img class="poster" ng-src="{{getPoster(movie.Title)}}" alt="{{movie.Title}}"> <div class="caption"> <h3>{{movie.Title}}</h3> <p>{{movie.Owner}}</p> </div> </div> </div> I know the HTTP

AngularJS : How to flatten this Promise chain?

家住魔仙堡 提交于 2019-12-06 05:18:28
问题 I have the following code: someService.fnReturnsPromise() .then(function () { return someService.fnReturnsAnotherPromise(someArg); }) .then(function (resultsOfSecondFn) { // do stuff with results }); I feel as if this should work; however, resultsOfSecondFn isn't actually the results, it's the promise itself that I returned. To make it work the way I want, I have to do this: someService.fnReturnsPromise() .then(function () { return someService.fnReturnsAnotherPromise(someArg); }) .then

AngularJS : How to use the $q promise feature in this situation to wait for data to be ready?

假装没事ソ 提交于 2019-12-06 00:23:18
I have a Controller which starts an API call in a Factory . Currently I start the call, then I have a function after that call to check the status of the Array . However I'm not sure how to force it to wait here while the data is being gathered, thus the need for some kind of $q implementation. As you can see in the screenshot below, vs.tickers returns an empty Object, or Array. Then finally the console.log in GetTickersFactory fires: 1st Controller if (root.tickerType === 'portfolio') { // Call to factory to start the API GETS: GetTickersFactory.getTickers('portfolio'); // I then call a

$q promises - Object is not a function

落花浮王杯 提交于 2019-12-05 19:08:54
I wanted to run the promises sample from https://docs.angularjs.org/api/ng/service/$q , here is my Code: angular.module('testControllers').controller('testCtrl', ['$scope', '$q', function($scope, $q) { function asyncGreet(name) { // perform some asynchronous operation, resolve or reject the promise when appropriate. return $q(function(resolve, reject) { setTimeout(function() { if (true) { resolve('Hello, ' + name + '!'); } else { reject('Greeting ' + name + ' is not allowed.'); } }, 1000); }); } var promise = asyncGreet('Robin Hood'); promise.then(function(greeting) { console.log('Success: ' +

AngularJs console.log “$q is not defined”

扶醉桌前 提交于 2019-12-05 16:36:40
问题 I am getting this error in the console $q is not defined . When I did some research I found some thing like .q library has been deprecated from http://www.breezejs.com/documentation/breeze-labs/breezeangularqjs If this is so, then the whole concept of promises is also deprecated, 回答1: Promises are not deprecated. In fact they're gaining quite a lot of momentum lately and are included in the next version of JavaScript. Let's look at what they say: This breeze.angular.q library has been

Updating array object values

删除回忆录丶 提交于 2019-12-05 14:35:32
I want to update some values in global array that I keep in a factory. I use the get method to get the data but set function somehow doesn't do its job and the value in the array doesn't get updated. What am I missing? .factory('messageList', function () { var Messages = [ { "title":"Cash in", "icon":"ion-social-euro", "dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0", "category": "financial", "active": "true" }, { "title":"Sales orders", "icon":"ion-social-euro", "dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0", "category": "sales", "active": "true" } ] return { get:

Angular/Jasmine testing with deffered promises

和自甴很熟 提交于 2019-12-05 13:22:27
I am testing a controller using a combination of angular and jasmine, and am not completely sure about using deffered promises. This is my spec code. describe('Controller Tests', function(){ var scope, searchAPI; beforeEach(function(){ var mockSearchAPI = {}; module('myApp', function($provide){ $provide.value('searchAPI', mockSearchAPI); }); }); inject(function($q){ var testData = {"message":"hi"}; mockSearchAPI.executeSearch = function(){ var defer = $q.defer(); defer.resolve(testData); return defer.promise; }; }); beforeEach('Main Search Controller Tests', function(){ function($controller,

Setting a timeout for each promise within a promise.all

久未见 提交于 2019-12-05 11:33:57
I am able to successfully perform a Promise.all, and gracefully handle resolves and rejects. However, some promises complete within a few milliseconds, some can/could take a while. I want to be able to set a timeout for each promise within the Promise.all, so it can attempt to take a maximum of say 5seconds. getData() { var that = this; var tableUrls = ['http://table-one.com','http://table-two.com']; var spoonUrls = ['http://spoon-one.com','http://spoon-two.com']; var tablePromises = that.createPromise(tableUrls); var spoonPromises = that.createPromise(spoonUrls); var responses = {}; var

How to use angular 2 service which returns http promise

若如初见. 提交于 2019-12-05 10:04:23
I have a trouble with angular 2 here. I use service that return promise but when i try to retrive the response i got an error. i was read this this stact question this my code. this is HotelService.ts import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; //rxjs promises cause angular http return observable natively. import 'rxjs/add/operator/toPromise'; @Injectable() export class HotelService { private BASEURL : any = 'http://localhost:8080/hotel/'; constructor(private http: Http) {} load(): Promise<any> { return this.http.get(this.BASEURL + 'api/client/hotel/load')

q.all not working for multiple promises

拥有回忆 提交于 2019-12-05 08:03:32
I have the following q.all calling to resolve two promises. I checked all the posts and tried all other ways of implementation q.all and its the same case var xyzdeffered = $q.defer(); service1.getServiceDetail1($routeParams.id).then(function(promise) { xyzdeffered.resolve(promise); }); var abcdeffered = $q.defer(); service2.getServiceDetail2($routeParams.id).then(function(promise) { abcdeffered.resolve(promise); }); $q.all([ xyzdeffered, abcdeffered ]).then(function(data) { $scope.variable = data; }); I am expecting the variable in q.all should get populated only after the first two promises