Questions tagged [angular2-services]

Use this tag for questions related to Angular 2 Services, which are JavaScript functions responsible for performing a single task.

Useful Links:

  1. Services - tutorial
2197 questions
320
votes
21 answers

What is the correct way to share the result of an Angular Http network call in RxJs 5?

By using Http, we call a method that does a network call and returns an http observable: getCustomer() { return this.http.get('/someUrl').map(res => res.json()); } If we take this observable and add multiple subscribers to it: let network$ =…
Angular University
  • 38,399
  • 15
  • 70
  • 79
254
votes
5 answers

What is the proper use of an EventEmitter?

I've read questions like Access EventEmitter Service inside of CustomHttp where the user uses EventEmitter in his service, but he was suggested in this comment not to use it and to use instead Observables directly in his services. I also read…
Eric Martinez
  • 28,976
  • 7
  • 86
  • 89
160
votes
3 answers

Angular2: How to load data before rendering the component?

I am trying to load an event from my API before the component gets rendered. Currently I am using my API service which I call from the ngOnInit function of the component. My EventRegister component: import {Component, OnInit, ElementRef} from…
Tom Aalbers
  • 3,630
  • 4
  • 21
  • 44
155
votes
14 answers

How do I create a singleton service in Angular 2?

I've read that injecting when bootstrapping should have all children share the same instance, but my main and header components (main app includes header component and router-outlet) are each getting a separate instance of my services. I have a…
Jason Goemaat
  • 27,053
  • 14
  • 78
  • 109
145
votes
7 answers

Angular 2 TypeScript how to find element in Array

I have a Component and a Service: Component: export class WebUserProfileViewComponent { persons: Person []; personId: number; constructor( params: RouteParams, private personService: PersonService) { …
trap
  • 2,160
  • 5
  • 16
  • 33
126
votes
7 answers

How to check the length of an Observable array

In my Angular 2 component I have an Observable array list$: Observable; In my Template I have
No records found.
117
votes
2 answers

Difference between EventEmitter.next() and EventEmitter.emit() in Angular 2

What is the difference between EventEmitter.emit() and EventEmitter.next()? Both dispatching the event to the subscribed listeners. export class MyService { @Output() someEvent$: EventEmitter = new EventEmitter(); someFunc() { …
Holger Stitz
  • 1,601
  • 3
  • 14
  • 19
99
votes
3 answers

How to avoid imports with very long relative paths in Angular 2?

How can I introduce something like 'my-app-name/services' to avoid lines like the following import? import {XyService} from '../../../services/validation/xy.service';
Thomas Zuberbuehler
  • 6,224
  • 12
  • 39
  • 68
84
votes
6 answers

How to bind static variable of component in HTML in angular 2?

I want to use a static variable of a component in HTML page. How to bind static variable of component with a HTML element in angular 2? import { Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs/Rx'; @Component({ …
Santosh Hegde
  • 2,520
  • 7
  • 25
  • 42
77
votes
3 answers

Property 'toPromise' does not exist on type 'Observable'

import { Headers, Http } from '@angular/http'; @Injectable() export class PublisherService{ private publishersUrl = 'app/publisher'; constructor(private http: Http) { } getPublishers(): Promise{ return…
MiHawk
  • 1,370
  • 1
  • 18
  • 28
67
votes
2 answers

How can I detect service variable change when updated from another component?

I'm trying to get the updated value from a service variable (isSidebarVisible) which is keeps on updated by another component (header) with a click event (toggleSidebar). sidebar.service.ts import { Injectable } from '@angular/core'; import {…
Body
  • 3,156
  • 8
  • 35
  • 47
63
votes
3 answers

Getting Image from API in Angular 4/5+?

I have new to develop Angular 4. I have facing issue while getting response from API about display image.In API,an image file has input-stream file,I don't know how to retrieve it and display it properly. Can you anyone resolve it? I tried…
Karthic G
  • 858
  • 1
  • 11
  • 23
51
votes
3 answers

angular 2 http withCredentials

I'm am trying to use withCredentials to send a cookie along to my service but can't find out how to implement it. The docs say "If the server requires user credentials, we'll enable them in the request headers" With no examples. I have tried several…
Lindstrom
  • 653
  • 1
  • 5
  • 7
50
votes
1 answer

Cant export service from module 'it was neither declared nor imported'

I am trying to export an Service from one of my modules but i only get the following error: ERROR Error: Uncaught (in promise): Error: Can't export value ConfirmDialogService from SharedModule as it was neither declared nor imported! My Module is…
Code Spirit
  • 2,197
  • 2
  • 10
  • 18
50
votes
1 answer

Pass config data using forRoot

I am trying to pass config data into a custom library in Angular. In the users application, they will pass some config data to my library using forRoot // Import custom library import { SampleModule, SampleService } from 'custom-library'; ... //…
Michael Doye
  • 7,070
  • 4
  • 35
  • 51
1
2 3
99 100