757
votes
10answers
174k views

Angular.js: service vs provider vs factory?

I'm sure this has bothered you - it has bothered me for some time now. What are the differences between AngularJS module's Service, Provider and Factory?
71
votes
6answers
18k views

angular js - configuration for different enviroments

how do you manage configuration variables/constant for different enviroments? This could be an example: my rest api it's reachable on localhost:7080/myapi/, but my friend that works on the same ...
64
votes
4answers
9k views

How can I test an an AngularJS service from the console?

I have a service like: angular.module('app').factory('ExampleService', function(){ this.f1 = function(world){ return 'Hello '+world; } return this; }) I would like to test it from the ...
50
votes
2answers
15k views

AngularJS: when to use service instead of factory

Please bear with me here. I know there are other answers such as: Angular.js: service vs provider vs factory? However I still can't figure out when you'd use service over factory. From what I ...
50
votes
3answers
18k views

AngularJS - The correct way of binding to a service properties

I’m looking for the best practice of how to bind to a service property in AngularJS. I have worked through multiple examples to understand how to bind to properties in a service that is created using ...
30
votes
4answers
41k views

How to access the services from RESTful API in my angularjs page?

I am very new to angularJS. I am searching for accessing services from RESTful API, but I didn't get any idea. How do that?
13
votes
3answers
3k views

AngularJS - $watch inside a service?

Is it possible to set up a $watch on an array of objects inside a service (I'd like the $watch declaration itself to be inside the service)?
13
votes
3answers
1k views

Should services expose their asynchronicity?

I'm writing a service that will retrieve data asynchronously ($http or $resource). I can hide the fact that it is asynchronous by returning an array that will initially be empty, but that will ...
11
votes
3answers
1k views

How can I extend $q promise in Angularjs with a .succes and .error

I wrote this little code in a custom service in AngularJS. In my service : var deferred = $q.defer(); var promise = deferred.promise; deferred.resolve('success'); ...
11
votes
2answers
4k views

How can I define an AngularJS service using a TypeScript class that doesn't pollute the global scope?

I am using AngularJS and TypeScript. I want to implement an AngularJS service using a Typescript class, like this: class HelloService { public getWelcomeMessage():String { return "Hello"; ...
10
votes
2answers
5k views

Inject dateFilter in a service in AngularJs

I would like to know if there is a way to inject the filters in a service in AngularJs. I've been trying app.factory('educationService', [function($rootScope, $filter) { // ..... Some code ...
10
votes
2answers
4k views

Stop $timeout when starting new controller

I'm polling for my data every 2 seconds to keep them updated on the page. My problem is when I visit an other page the timeout stays active. How can i cancel my timeout when I visit an other page? ...
10
votes
1answer
6k views

angularjs $http.get to get json not working in the service layer

I am developing an angularjs app as a part of my angularjs learning. I have controllers and from there I am calling service layers. leagueManager.service("teamsService", function($http){ var ...
9
votes
1answer
1k views

How to use two AngularJS services with same name from different modules?

Supposed I have two modules for AngularJS, e.g. foo and bar, and both of them define a service called baz. In my application I depend on them by saying: var app = angular.module('app', [ 'foo', ...
9
votes
2answers
2k views

Call angularjs service from simple js code

I have the following angularjs service: angular.module('app.main').factory('MyService', ["$http", function ($http) { return new function () { this.GetName = function () { ...
8
votes
1answer
6k views

Testing a simple AngularJS service using Jasmine

I have a simple service that I am trying to unit test. No matter what I try, either the searchService is an unknown provider, or service is null (which oddly enough won't cause my test to fail!!). ...
8
votes
2answers
779 views

Angular Resource - how to check if a resource instance has any unsaved changes?

I want to be able to tell whether an $resource instance has been modified by the user - that is, whether its current state is different than what has been initially loaded from the server && ...
8
votes
1answer
3k views

AngularJS unexpected token in fromJson()

The following line of code: var sid = $cookieStore.get('PHPSESSID'); is throwing this error: SyntaxError: Unexpected token m at Object.parse (native) at Object.fromJson ...
8
votes
1answer
3k views

Getting AngularJS Error: “[$rootScope:inprog] $digest already in progress” without a manual $apply

Other posts on this error always include someone trying to $apply without using a safe apply, but that's not the case in my example. My function IS successfully returning the data I requested from the ...
7
votes
2answers
2k views

AngularJS, is this way of using service good?

i've this HTML: <p>Hello {{name}}</p> and the controller is: function myCtrl(scope, service) { scope.name = service.getUsername(); // service.getUsername() return "World!" } ...
7
votes
4answers
4k views

Delay an angular.js $http service

I have some angular factories for making ajax calls towards legacy ASP.NET .asmx web services like so: module.factory('productService', ["$http", function ($http) { return { ...
7
votes
2answers
8k views

AngularJS: How to handle success and error call backs with ngResource?

The docs does not give any idea about it. My REST enpoint might throw error $scope.delete = function(index) { Transaction.delete({transactionId: $scope.transactions[index].uuid}) }; I ...
7
votes
2answers
3k views

Cordova + Angularjs + Device Ready

I am developing a mobile application using Cordova and AngularJS. How do I restrict bootstrapping of AngluarJS before Cordova device ready. Basically I don't want to use any of AngularJS controllers ...
7
votes
3answers
8k views

AngularJS: How to pass values from Controller to Service Method?

I have a controller which is dependent on a TransactionService. One of the method is $scope.thisMonthTransactions = function () { $scope.resetTransactions(); var today = new Date(); ...
7
votes
1answer
246 views

AngularJS philosophy - controllers as “windows” to services

Sorry for the vague title; I've been restructuring some of my AngularJS code, trying to be more "Angular" about it, and I've noticed this pattern popping up quite a bit: app.service("someService", ...
6
votes
2answers
8k views

How to wait till the response comes from the $http request, in angularjs?

I am using some data which is from a RESTful service in multiple pages. So I am using angular factories for that. So, I required to get the data once from the server, and everytime I am getting the ...
6
votes
1answer
484 views

The better approach to design AngularJS services

I'm writing an AngularJS client application that would interact with a REST server. To manage the client / server interaction I'm using the $resource abstraction. Actually I'm writing every resource ...
6
votes
2answers
5k views

Force AngularJS service to return data before loading controller

I have a service in Angular which uses my API to get user information and provides it to my controllers. It's set up like this: angular.module('myApp', ['myApp.filters', 'myApp.services', ...
6
votes
1answer
4k views

AngularJS Directive not able to access isolate scope objects

I am trying to put some default values in my directive with Isolate scope. Basically, I need to do some DOM manipulations using the scope object when my directive is bound. Below is my code: ...
6
votes
1answer
483 views

Angular Service Definition: service or factory

I am an angular newbie, I am building an application, one thing really puzzling me is there are couple of ways of defining a service, and I read more from this link: How to define service then it ...
6
votes
1answer
3k views

Angularjs - how do I set module values in a service?

I have the following services module for an Angular app. angular.module('rs.services', []) .value('uid', null) .factory('login', ['$http', 'uid', function($http, uid) { return function(user, ...
6
votes
1answer
3k views

AngularJS factory property isn't being updated in $scope when not using push()

I have a factory that is retrieving the data from an external source. As soon as i get the data, i use a second factory to filter it by a certain criteria. The factory property is assigned to scope. ...
6
votes
2answers
6k views

Passing argument(s) to a service in AngularJs

I am trying to configure my first tidbits of the AngularJs for a trivial stuff, but unfortunately unsuccessful at it after considerable amount of time. My Premise: Users select one of the options ...
5
votes
3answers
7k views

AngularJS factory http returns empty

I'm trying AngularJS for the first time. I'm getting JSON data from a http-get request using a factory, but the object is returned empty, before the ajax-request is done. Factory: ...
5
votes
2answers
2k views

Firebase's AngularFire in an AngularJS service

The best way of handling Firebase in AngularJS surely has to be from within a service, so it's available to all Controllers across the App. I just can't get it to work! ... I first tried using ...
5
votes
1answer
2k views

Is CacheFactory in angularjs a singleton?

I've created a service using the CacheFactory. I was expecting it to be a singleton. I inject it into my controller and it works fine within the scope of the controller. But once I go to a different ...
5
votes
4answers
4k views

How to mock $window.location.replace in AngularJS unit test?

I've got the following service: angular.module("services") .factory("whatever", function($window) { return { redirect: function() { $window.location.replace("http://www.whatever.com"); ...
5
votes
2answers
705 views

Reusable AngularJS Service within an App

I'm very new to AngularJS, so it's possible I'm asking the entirely wrong question. What I'm trying to accomplish is create a reusable class with data binding in a single app. The following ...
5
votes
1answer
9k views

How to enable cors request with angular.js-resource

I have an angular.js application and i need to do CORS request. I want to define my rest services "the angular" using angular resources, described here: http://docs.angularjs.org/tutorial/step_11. ...
5
votes
2answers
3k views

AngularJS inject service mock inside service tests

I have been trying to test a service to no avail for some time now and was hoping for some help. Here is my situation: I have a service looking a little like this myModule.factory('myService', ...
5
votes
2answers
1k views

Best practice for dependency injection in an AngularJS service with TypeScript

I find dependency injection for AngularJS services in TypeScript to be somewhat cumbersome. Currently, I define a factory method inside my service class, and have to repeat all dependency injection ...
5
votes
1answer
902 views

How to “eager load” a service in AngularJS? (instantiate it before its needed, automatically)

I'm trying to achieve a program structure like this: The problem here is, when there is no apparent controller using the Features in the beginning, they are not instantiated and not registered in ...
4
votes
2answers
771 views

Injecting a service into another service in angularJS

Is it possible to inject one service into another service in angularJS?
4
votes
3answers
8k views

Simple Angular $routeProvider resolve test. What is wrong with this code?

I have created a simple Angular JS $routeProvider resolve test application. It gives the following error: Error: Unknown provider: dataProvider <- data I would appreciate it if someone could ...
4
votes
2answers
9k views

Uploading a file with AngularJS and bluimp on success callback of another form

I have followed the following tutorial in order to integrate the notorious bluimp jQuery file uploader in my AngularJS project. After some research I found that in the options array, witihn the ...
4
votes
3answers
4k views

Angular modal dialog best practices

What is the best practice for creating modal dialogs with dynamic content, contrasted with dialogs that don't have dynamic content. Eg.. We have some modal forms that accept a list of form elements, ...
4
votes
1answer
7k views

how to call service method from ng-change of select in angularjs?

I am new to angular js. I am trying to call factory service method 'getScoreData' from ng-change of select, but not able to get it done. please help. Html code: <select ng-model="Score" ...
4
votes
2answers
621 views

Angular directive not picking up service data

My service NavData angular.module('navData', []). factory('NavData', function() { var navData = {}; navData.depth = 0; navData.category_id = ""; ...
4
votes
1answer
2k views

AngularJS - How do I avoid using $timeout to wait for an element to be created?

Here is the idea: So I am trying to use an external library function to create some manipulation in the DOM in one of my controllers that my ng-repeat is connected to. Newest jsFiddle working but ...
4
votes
1answer
502 views

Can .$inject be used on Services in AngularJS, or is it needed only for Controllers?

I'm aware that for the purposes of minification and obfuscation we should always use the $injector (by way of controllerName.$inject = ['$service', '$service2']) to specify the actual service names ...