Questions tagged [angular-services]

Angular services are substitutable objects that are wired together using dependency injection.

Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.

are:

  • Lazily instantiated: Angular only instantiates a service when an application component depends on it.
  • Singletons: Each component dependent on a service gets a reference to the single instance generated by the service factory.

Reference

https://docs.angularjs.org/guide/services

2054 questions
1081
votes
9 answers

angular.service vs angular.factory

I have seen both angular.factory() and angular.service() used to declare services; however, I cannot find angular.service anywhere in official documentation. What is the difference between the two methods? Which should be used for what (assuming…
Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
424
votes
21 answers

AngularJS : How to watch service variables?

I have a service, say: factory('aService', ['$rootScope', '$resource', function ($rootScope, $resource) { var service = { foo: [] }; return service; }]); And I would like to use foo to control a list that is rendered in HTML:
berto
  • 8,215
  • 4
  • 25
  • 21
262
votes
9 answers

@HostBinding and @HostListener: what do they do and what are they for?

In my meanderings around the world wide interweb, and now especially the angular.io style docs, I find many references to @HostBinding and @HostListener. It seems they are quite fundamental, but unfortunately the documentation for them at the moment…
serlingpa
  • 12,024
  • 24
  • 80
  • 130
251
votes
18 answers

Passing data between controllers in Angular JS?

I have a basic controller that displays my products, App.controller('ProductCtrl',function($scope,$productFactory){ $productFactory.get().success(function(data){ $scope.products = data; }); }); In my view I'm displaying this…
kishanio
  • 6,979
  • 8
  • 25
  • 33
180
votes
3 answers

What is the difference between the $parse, $interpolate and $compile services?

What is the difference between $parse, $interpolate and $compile services? For me they all do the same thing: take template and compile it to template-function.
Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
139
votes
8 answers

Angular 4+ ngOnDestroy() in service - destroy observable

In an angular application we have ngOnDestroy() lifecycle hook for a component / directive and we use this hook to unsubscribe the observables. I want to clear / destory observable that are created in an @injectable() service. I saw some posts…
mperle
  • 3,342
  • 8
  • 20
  • 34
106
votes
4 answers

Passing current scope to an AngularJS Service

Is it correct to pass the "current" $scope to an AngularJS service? I'm in the situation where I've a $service knowing it's consumed by only one controller, and I'd like to have a reference to the controller's scope in the $service methods…
S.C.
  • 2,844
  • 3
  • 26
  • 27
91
votes
4 answers

use $http inside custom provider in app config, angular.js

The main question - is it possible? I tried with no luck.. main app.js ... var app = angular.module('myApp', ['services']); app.config(['customProvider', function (customProvider) { }]); ... provider itself var services =…
Kosmetika
  • 20,774
  • 37
  • 108
  • 172
60
votes
2 answers

What is the lifecycle of a service in Angular 5

Angular 5 When is a service created and destroyed, what are its lifecycle hooks (if any) and how is its data shared between components? EDIT: To clarify, this is NOT a question about the lifecycle of components. This question is about the lifecycle…
rahs
  • 1,759
  • 2
  • 15
  • 31
58
votes
1 answer

The view is not updated in AngularJS

Updating the model property has no effect on the view when updating the model in event callback, any ideas to fix this? This is my service: angular.service('Channel', function() { var channel = null; return { init:…
petrov.alex
  • 1,089
  • 2
  • 12
  • 20
56
votes
3 answers

AngularJS - UI Router - programmatically add states

Is there a way to programmatically add states to $stateProvider after module configuration, in e.g. service ? To add more context to this question, I have a situation where I can go with two approaches: try to force the reload on the state defined…
khorvat
  • 1,630
  • 2
  • 20
  • 31
49
votes
2 answers

How to use angular2 built-in date pipe in services and directives script files

I want to use angular2's date pipe in services and directives script files(not only in HTML). Does anyone have any ideas? Can't upload code cos some policy restrictions, sorry about that.
Chen Dachao
  • 1,736
  • 2
  • 21
  • 36
48
votes
5 answers

Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService'. Found string

While working on a program that I am facing an issue with, constructor and its dependency injection from its child class. DataService: A service class which has all the CRUD operation at one place and also has a parameterized constructor for…
Seemant Shukla
  • 619
  • 1
  • 7
  • 16
47
votes
7 answers

Get status code http.get response angular2

I need to get the status code of the following http call and return it as a string //This method must return the status of the http response confirmEmail(mailToken):Observable{ return…
47
votes
5 answers

Correct way Provide DomSanitizer to Component with Angular 2 RC6

I'm attempting to use DomSanitizer to sanitize a dynamic URL within a Component using I can't seem to figure out what the correct way to specify a Provider for this service is. I'm using Angular 2.0.0-rc.6 Here's my current component: @Component({ …
1
2 3
99 100