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
33
votes
7 answers

Angular (v5) service is getting constructed before APP_INITIALIZER promise resolves

I'm expecting Angular to wait until my loadConfig() function resolves before constructing other services, but it is not. app.module.ts export function initializeConfig(config: AppConfig){ return () => config.loadConfig(); } @NgModule({ …
32
votes
2 answers

difference between setTimeout in javascript and $timeout service in angularjs

Iam new to angular framework.Here is my scenario where, I want to change my $scope.variable after a period of time so i used javascript setTimeout method. $scope.variable = 'Previous'; setTimeout(function(){ $scope.variable='NEXT'; },3000); This…
31
votes
3 answers

How to test 'private' functions in an angular service with Karma and Jasmine

I have a service in my angular app that looks something like this: angular.module('BracketService', []).factory('BracketService', [function() { function compareByWeight(a, b) { return a.weight - b.weight; } function…
paniclater
  • 903
  • 1
  • 12
  • 18
30
votes
5 answers

Invalid provider for the NgModule 'DynamicTestModule' when testing a service in Angular 2

I have the following service: import { Injectable } from '@angular/core'; import { MenuItem } from './../classes/menu-item'; import { ITEMS } from './../static-data/items-list'; @Injectable() export class ItemsListService { getItems():…
Cristian
  • 1,590
  • 5
  • 23
  • 38
26
votes
4 answers

Angular v4: Do we store data in a Service or the Component or both?

Angular v4: Do we store data in a Service or the Component or both? After reviewing quite a few tutorials, as well as reading the documentation of Angular, I'm still not clear on this subject. https://angular.io/tutorial/toh-pt2 Angular's tutorial…
Speros
  • 361
  • 2
  • 4
  • 10
26
votes
2 answers

Subscribe is not a function error

I am trying to subscribe to an observable from a service, it builds without error but I get the error "this.service.getBanners(...).subscribe is not a function" when viewing in the browser. Service: import { Injectable } from '@angular/core'; import…
Steve
  • 4,314
  • 3
  • 16
  • 21
24
votes
4 answers

Angular cli service generation: service is generated in root instead of /services

I'm messing around with Angular a bit. Trying to generate some stuff with angular-cli. But when generating a new service the service is placed in "/src/" folder. I would like to set a global default folder for this. Is there a config setting where…
Mike Bovenlander
  • 5,236
  • 5
  • 28
  • 47
23
votes
2 answers

Angular Service singleton constructor called multiple times

I am trying to use an app-wide service (UserService) that stores authenticated user details. I have set up some routes but found that UserService is instantiated per route. I want them to share the same UserService. I have created a CoreModule…
23
votes
4 answers

Angular: 'Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays'

I've created an angular app which gets data from a json file. But I'm having issues with showing the data in html. A lot of variables are in dutch, I'm sorry for that. I'm also a bit new to all of this :) This is my service: import {Injectable} from…
Claire
  • 475
  • 1
  • 8
  • 21
22
votes
3 answers

What is the difference between providedIn any and root

In Angular 9 the injectable decorator option providedIn has a new value called any. What is the difference between root and any? Is a service considered a singleton in the case that I use any? @Injectable({providedIn: 'any'}) class UsefulService…
22
votes
2 answers

Observable vs Subject and asObservable

I am learning RxJs, I am seeking confirmation or correction on my assumption. I am trying to make a public read only observable in a service that I can use .next() on in various places in my service class. I am wondering if this is the proper way to…
user9298624
22
votes
4 answers

How to update dependency injection token value

Angular dependency injection let you inject a string, function, or object using a token instead of a service class. I declare it in my module like this: providers: [{ provide: MyValueToken, useValue: 'my title value'}] and I use it like…
20
votes
5 answers

How to keep globally current user until logout with angular_devise?

How to create own service accessible globally that will call server by currentUser() only once on page load, if User is logged in then keep it and provide data to controllers or states until logout? Now I'm resolving many times in many states or…
luzny
  • 2,380
  • 7
  • 30
  • 64
19
votes
3 answers

Seperate instances of an angular service per (set of) component(s)

TLDR: How do I use one instance of an Angular (6) service for one set of (instances of) components (and directives) and another one for another set of the same components. Or with more information: I am currently working on adding a sorting feature…
CodeO
  • 645
  • 1
  • 7
  • 16
19
votes
4 answers

Unit testing with private service injected using jasmine angular2

I have a problem trying to unit test an angular service. I want to verify that this service is properly calling another service that is injected into it. Lets say I have this ServiceToTest that injects ServiceInjected: ServiceToTest…
RjHiruma
  • 193
  • 1
  • 1
  • 4