Questions tagged [angularjs-service]

AngularJS services are singletons that carry out specific tasks common to web apps. AngularJS provides a range of built-in services, along with the ability to create custom services as required. Services are also used for communication between application components through dependency injection (DI).

Service

  • Gives us the instance of a function (object) you just instantiated with the new keyword and you’ll add properties to this and the service will return this. When you pass the service into your controller, those properties on this will now be available on that controller through your service (hypothetical scenario).

  • Singleton and will only be created once

  • Usage: if you're using a class you could use the service provider

  • Syntax: module.service('serviceName', *func reference or anonymous*);

Services are a feature that Angular brings to client-side web apps from the server side, where services have been commonly used for a long time. Services in Angular apps are substitutable objects that are wired together using dependency injection (DI).

The Angular web framework provides a set of services for common operations. Like other core Angular variables and identifiers, the built-in services always start with $ (such as $http).
You can also create your own custom services.

Services are also used for communication between application components through DI, and can be accessed from controllers, directives, filters and other services.

AngularJS .service


module.service('MyService', function() {

    this.method1 = function() {
        //...
    }

    this.method2 = function() {
        //...
    }
});
1527 questions
3410
votes
30 answers

AngularJS: Service vs provider vs factory

What are the differences between a Service, Provider and Factory in AngularJS?
403
votes
4 answers

How can I test 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 JavaScript console and call the function f1() of the…
justGoscha
  • 24,085
  • 15
  • 50
  • 61
301
votes
9 answers

AngularJS : When to use service instead of factory

Please bear with me here. I know there are other answers such as: AngularJS: Service vs provider vs factory However I still can't figure out when you'd use service over factory. From what I can tell factory is commonly used to create "common"…
Justin Khoo
  • 3,261
  • 3
  • 15
  • 13
222
votes
10 answers

How do I configure different environments in Angular.js?

How do you manage configuration variables/constants for different environments? This could be an example: My rest API is reachable on localhost:7080/myapi/, but my friend that works on the same code under Git version control has the API deployed…
rbarilani
  • 2,656
  • 3
  • 16
  • 17
221
votes
4 answers

AngularJS : Factory and Service?

EDIT Jan 2016: Since this still gets attention. Since asking this I've completed a few AngularJS projects, and for those I mostly used factory, built up an object and returned the object at the end. My statements below are still true, however. EDIT…
Cameron Ball
  • 4,048
  • 6
  • 25
  • 34
164
votes
10 answers

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 AngularJS. Below I have two examples of how to…
115
votes
7 answers

Injecting a mock into an AngularJS service

I have an AngularJS service written and I would like to unit test it. angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']). factory('myService', function ($http, fooService, barService) { this.something =…
BanksySan
  • 27,362
  • 33
  • 117
  • 216
112
votes
25 answers

Angularjs: Error: [ng:areq] Argument 'HomeController' is not a function, got undefined

This is my demo using angularjs, for creating a service file, and adding service to a controller. I have two problems with my demo: One is when I put