angular-promise

AngularJS ng-route how to make http request on resolve

巧了我就是萌 提交于 2019-12-20 05:18:34
问题 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

How to bind content to individual div with promise

百般思念 提交于 2019-12-20 05:15:58
问题 This plnkr : https://plnkr.co/edit/BjETLN7rvQ1hNRIm51zG?p=preview binds content to three divs within loop : <div ng-repeat="id in ids"> src : { "content" : "divContent" , "id" : "r1" } <!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> <script src="script.js"></script> </head> <body> <div ng-controller="FetchCtrl"> <div ng-repeat="id in ids"> <div ng-bind="content" ></div> </div> </div> </body> </html> // Example

AngularJS : Testing factory that returns $http promises

妖精的绣舞 提交于 2019-12-20 05:13:49
问题 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

Angular chaining promises from foreach loop

流过昼夜 提交于 2019-12-20 04:38:52
问题 I have an array of photo files that needed to upload to Azure Cloud Storage, and i using foreach loop to call upload as below: $scope.savetemplate = function () { var imagePathsArray = []; $scope.filesimage = []; $scope.filesimage.push($scope.file1); $scope.filesimage.push($scope.file2); $scope.filesimage.push($scope.file3); for (var i in $scope.filesimage) { $scope.upload($scope.filesimage[i]); } $scope.data.Images = imagePathsArray ; $http({ //after finish uploads i need to post the paths /

angularjs simple .then or $q service in async requests

送分小仙女□ 提交于 2019-12-20 04:24:28
问题 I don't know what is the exactly difference between AngularJS $q service and simply using .then() after async request. Simple example with .then() : function InboxService($http) { this.getEmails = function getEmails() { return $http.get('/emails'); }; } And when using the service (just part of code): InboxService.getEmails() .then(function (response) { // use response }); What is the difference with $q service with resolve and reject ? 回答1: What is the difference with $q service with resolve

AngularJS Sequentially Chain Promises in forEach Loops

☆樱花仙子☆ 提交于 2019-12-20 04:10:25
问题 I have been doing lots of searching trying to find a solution, and believe it ultimately comes down the the promise as my data is returned but all at the end, where as I need it through each iteration. I have the vm.planWeek.dinner that I loop through each row, and append 'menuType' and the 'trackMenuIds' array to it, which I then use in my MongoDB call for search criteria. This all works fine, but the key element, is with each factory call returned, I add the returned item's id to the

Create promise in AngularJS

谁说胖子不能爱 提交于 2019-12-20 02:28:27
问题 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) {

Aborting ngResource using a promise object

拟墨画扇 提交于 2019-12-19 21:50:40
问题 I've recently learned that ngResource request can be aborted either by specifying a timeout in ms or passing a deferred object. The second solution does not seem to work for me, and I have no idea what I'm doing wrong. I've created a fiddle to demonstrate the problem http://jsfiddle.net/HB7LU/10977/ var myApp = angular.module('myApp',['ngResource']); myApp.factory('myResource', function($resource) { return { getResource: function (aborter) { var resource = $resource( 'http://api

$q.all and nested promises

喜你入骨 提交于 2019-12-19 18:49:13
问题 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

How to destroy unresolved promise

老子叫甜甜 提交于 2019-12-19 05:23:38
问题 Have a look into the code snippet $scope.getSongs = function(keyword){ songServices.getSongList(keyword).then( function(resp){ $scope.songList = resp.data.songList; } ); } Here getSongList simply returns list of songs from server by an HTTP request. And in my HTML: <input auto-focus type="text" placeholder="Enter song ID/Keyword" ng-model="keyword" ng-change="getSongs()"> The problem here is with behaviour of promises, sometimes if some promise takes more time(even in ms.) to get resolved