angular-promise

AngularJS : Testing factory that returns $http promises

徘徊边缘 提交于 2019-12-02 04:14:12
Trying to test an angular service that returns an $http GET request and the then handler, but I'm not able to test that the logic actually works inside of the then function. Here is a basic, truncated version of the service code: angular.module('app').factory('User', function ($http) { var User = {}; User.get = function(id) { return $http.get('/api/users/' + id).then(function (response) { var user = response.data; user.customProperty = true; return user; }); }; return User; }); And here is the test: beforeEach(module('app')); beforeEach(inject(function(_User_, _$q_, _$httpBackend_, _$rootScope

AngularJS ng-route how to make http request on resolve

别说谁变了你拦得住时间么 提交于 2019-12-02 03:48:45
I have a simple template with an input and an anchor link which, upon pressing it, will load another template. I want this second template to fetch the information via a http request. I am using ng-route to redirect to the search template by default and to the results template from the path /search/:title, and I am trying to use resolve to make the request before loading the results template ( code in plunker ) The main problem I am facing is that when I add the resolve the controller stops being initialized (I guess it will load after the promise is returned). This means variables such as the

Return data from http inside function

寵の児 提交于 2019-12-01 21:49:53
问题 I'm using this and this reference, to return data from an http-request inside a function: function getdetails(id) { var datafordetails = { data1: { item1: "", item2: "" }, data2: { item3: "", ID: id } }; var jsonDataDetails = JSON.stringify(datafordetails); return $http({ url: "http://...", method: "POST", data: jsonDataDetails, dataType: "json", timeout: 5000, contentType: 'application/json; charset=utf-8' }) .then(function (res) { var data = res.data; responsedata=data.d; return

Return data from http inside function

ぃ、小莉子 提交于 2019-12-01 21:15:55
I'm using this and this reference, to return data from an http-request inside a function: function getdetails(id) { var datafordetails = { data1: { item1: "", item2: "" }, data2: { item3: "", ID: id } }; var jsonDataDetails = JSON.stringify(datafordetails); return $http({ url: "http://...", method: "POST", data: jsonDataDetails, dataType: "json", timeout: 5000, contentType: 'application/json; charset=utf-8' }) .then(function (res) { var data = res.data; responsedata=data.d; return responsedata; },function (error) { $scope.status = status; console.log("Unable to update. No internet?"); }) } My

Create promise in AngularJS

a 夏天 提交于 2019-12-01 20:37:15
I'm trying to create a promise in Angular with the $q service. It returns an object retrieved from a web service. If the object is in the cache, it returns it without calling the web service. The problem is that the two resolves are getting called. Maybe, Am I using a promise anti-pattern? Here is my code: function returnMapAsync() { return $q(function (resolve, reject) { if (navigationMap) { resolve(navigationMap); } else { ServerRequest.getNavigationMap().then(function (data) { navigationMap = data.object; resolve(navigationMap); }); } }); } Thank you You shouldn't need to wrap everything in

Replacing $http with Fetch API

给你一囗甜甜゛ 提交于 2019-12-01 20:16:21
问题 I'm replacing $http with Fetch API and got replaced $q with Promise API. Because of that, Angular didn't run digest cycles anymore, thus UI didn't render. To solve this problem I tried Zone.js and that seems to solve our problems partially. Unfortunately its API completely changed in 0.6 so we're using legacy 0.5.15. Now to the actual problem. When refreshing the page Angular configs and bootstraps the application like expected. In this phase I'm initializing the Zone and decorating the

Angular2 Tutorial: Promise error (type not assignable) in Service

て烟熏妆下的殇ゞ 提交于 2019-12-01 19:00:00
I'm following the tutorial " Angular Tour of Heroes " at the Angular.io site. I've setup the project with Angular CLI (see environment versions at the end of this post). I'm stuck at point 5 (services) : using the same files provided by the tutorial, I get the following error in line 9 of 'hero.service.ts': Failed to compile. /home/myuser/angular-tour-of-heroes/src/app/hero.service.ts (9,4): Type 'Hero[]' is not assignable to type 'Promise<Hero[]>'. Property 'then' is missing in type 'Hero[]'. This is the code of the service (file 'hero.service.ts'): import { Injectable } from '@angular/core';

Replacing $http with Fetch API

╄→гoц情女王★ 提交于 2019-12-01 18:40:07
I'm replacing $http with Fetch API and got replaced $q with Promise API. Because of that, Angular didn't run digest cycles anymore, thus UI didn't render. To solve this problem I tried Zone.js and that seems to solve our problems partially. Unfortunately its API completely changed in 0.6 so we're using legacy 0.5.15 . Now to the actual problem. When refreshing the page Angular configs and bootstraps the application like expected. In this phase I'm initializing the Zone and decorating the $rootScope.apply with the Zone and $rootScope.$digest() . Now when I transition between states/routes (with

$q.all and nested promises

六眼飞鱼酱① 提交于 2019-12-01 17:49:01
Have a question about synchronizing nested promises when using $q in Angular. Will the following code ensure that the entire chain of promises is waited for? Meaning will the nested calls to services that return promises be waited for in the $q.all block? var call1 = service1.get('/someUr').then(function(){ return service2.get('/someUrl2'); //returns promise }); var call2 = service3.get('/someUr').then(function(){ return 'hello'; }); var call3 = service4.get('/someUr').then(function(){ return service3.get('/someUrl3');//returns promise }); $q.all(call1,call2,call3).then(function(){ console.log

Angular2 Tutorial: Promise error (type not assignable) in Service

亡梦爱人 提交于 2019-12-01 17:31:16
问题 I'm following the tutorial "Angular Tour of Heroes" at the Angular.io site. I've setup the project with Angular CLI (see environment versions at the end of this post). I'm stuck at point 5 (services) : using the same files provided by the tutorial, I get the following error in line 9 of 'hero.service.ts': Failed to compile. /home/myuser/angular-tour-of-heroes/src/app/hero.service.ts (9,4): Type 'Hero[]' is not assignable to type 'Promise<Hero[]>'. Property 'then' is missing in type 'Hero[]'.