angularjs-factory

AngularJS : How would i return value from factory if the data is fetched with http

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 14:31:44
问题 I have this factory in angular: 'use strict'; angular.module('finansiiApp') .factory('transactions', function ($http) { var transactions = []; $http.get("/api/transactions.json") .success(function(data, status){ transactions = data; }); // Public API here return { getTransactions: function () { return transactions; }, addTransaction: function(transaction){ transactions.push(transaction); } }; }); This is my controller: 'use strict'; angular.module('finansiiApp') .controller('MainCtrl',

Angularjs - Restangular call inside factory/service

本小妞迷上赌 提交于 2019-12-22 17:18:41
问题 I am using factory as follows: var appModule = angular.module('appModule', ['ngRoute','restangular']); appModule .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/home', { templateUrl: 'home.html', controller: 'ngHomeControl' }) .when('/contacts', { templateUrl: 'contacts.html', controller: 'ngContactControl' }); }]); appModule .factory('testFactory', function testFactory(Restangular) { return { getFriendList: function() { return Restangular .all('api/users

AngularJS - refresh view after http request, $rootScope.apply returns $digest already in progress

杀马特。学长 韩版系。学妹 提交于 2019-12-22 10:45:37
问题 Hey guys so I am simply trying to load data when my app starts. However, the view loads faster than the http request(of course). I want to refresh my view once my data has been properly loaded because that data defines my view. I've tried $rootScope.apply from inside the factory where I do my http request, and I also tried directly doing the http request in my controller again with $scope.apply, and neither one worked as they both gave me "$digest already in progress" Any idea how can I set

how to mock the backend for angularjs app?

那年仲夏 提交于 2019-12-22 10:45:07
问题 I would like to test a factory method that runs $resource. My test would mock the real backend that does not exist yet. Here is a sample factory code: app.factory( 'Global', function( $resource ){ var Jogas = $resource('/jogasok/:id', {'id': '@id'}, { 'ujBerlet': {'method': 'POST', 'params': {'berlet': true}} }); var jogasok = Jogas.query(); return { getJogasok: function() { return jogasok; } }; }) My test would be to check that query was made. If I initialize my app with: app.run( function

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

允我心安 提交于 2019-12-22 10:05:13
问题 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') { //

How to set a timeout to abort an $http.get() inside a factory or service?

不问归期 提交于 2019-12-21 12:15:19
问题 I have the following method getData(url) in a my factory which uses $http.get(url) to get data from an URL angular .module('az-app') .factory('WebServiceFactory', function ($http, $q) { var WebServiceFactory = this; WebServiceFactory.getData = function (url) { var deferred = $q.defer(); $http.get(url) .then( function (response) { deferred.resolve({ data: response }); }, function (rejected) { deferred.reject({ data: rejected }); } ); //Promise to be returned return deferred.promise; } It works

AngularJS - Dependency injection involving asynchronous data

自闭症网瘾萝莉.ら 提交于 2019-12-13 16:24:57
问题 I want to make the currently logged in user's ID and user name available to my Angular directives. I have created an API end-point to retrieve this information (along with some other information). The problem is that the API call is asynchronous: var url = baseUrl + 'api/sessions'; $http.get(url) I am trying to create a factory that will return the JSON object returned by the API. However, since the call is asynchronous, I don't know how to implement the factory method. Additionally, I don't

What is the best approach for integrating AngularJS with a non-RESTful API?

谁都会走 提交于 2019-12-13 08:54:09
问题 I have a REST API which has something like this: GET /zones - lists all zones { "zones": [ { "name": "zone 1", "persons": [ 0, 2, 3 ], "counter" : 3 }, { "name": "zone 2", "persons": [ 1, 5 ], "counter" : 0 } ] } POST /zones - creates a new zone { "name": "zone 1", "persons": [ 0, 2, 3 ] } DELETE /zones/:id deletes a zone PUT /zones/:id updates a zone and now, finally I have this: GET /zones/increment_counter/:id which increments the counter parameter of the zone. I am using Angular and I am

Modified data in a factory that's bound to a controller's scope, it doesn't update

百般思念 提交于 2019-12-13 01:13:33
问题 I have a pretty simple example (also available on this fiddle: http://jsfiddle.net/depov5w6/) I have 2 factories, one which returns an object with the property 'data', (which is bound to the scope of a controller) and another factory that has 2 methods: populateData and pushData. populateData sets the value of dataSource.data to be a new value. pushData pushes a new value onto dataSource.data angular.module("app",[]) .factory('dataSource', function() { return { data: ["Init Data"] }; })

Angular store value to service to acces on multiple pages

余生颓废 提交于 2019-12-12 09:56:59
问题 SOLUTION: It seemed that my .get() function invoked before I could update the variable 'something' and therefore it didn't show the updated variable. When I tested it on the real page it worked like a charm :) final FIDDLE note: I did add a return to the set function to immediately update the view. UPDATE: got my factory working alright but can't get the value bound to the factory .set() function. FIDDLE I’m building a manual to install a USB-network connector and need to store some variables