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
2204 questions
31
votes
3 answers

How to call component method from service? (angular2)

I want to create service, which can interact with one component. All another components in my app, should be able to call this service, and this service should interact with this component. How to call component method from service? @Component({ …
31
votes
2 answers

How to subscribe to event emitter once?

// Part of service public someEvent: EventEmitter = new EventEmitter(); .... // Component @Component({ selector: 'some-component', template: `...` }) export class SomeComponent { constructor(public service: Service) { …
Eggy
  • 4,052
  • 7
  • 23
  • 39
30
votes
4 answers

Property 'subscribe' does not exist on type '() => Observable'

service file import { Observable } from 'rxjs/Rx'; import { Http,Response} from '@angular/http'; import { Injectable } from '@angular/core'; import 'rxjs/add/operator/Map'; @Injectable() export class VideoService { private geturl =…
HASSAN MEHMOOD
  • 343
  • 2
  • 5
  • 11
30
votes
2 answers

Angular 2 Component listen to change in service

I've a simple question about change detection. I have a component and a (global) service with a boolean inside. How can I make the component listen to that boolean and execute a function if that boolean changes? Thanks!
Han Che
  • 8,239
  • 19
  • 70
  • 116
29
votes
3 answers

Angular 2: Difference between service and redux

What is the need of redux because we can also save and get data from services, as far as I understand we can also get and save data into services and those services could be used by other components.
blackHawk
  • 6,047
  • 13
  • 57
  • 100
28
votes
4 answers

How to get Form Data in Angular 2

I have a form in my angular 2 Project. I know how to Retrieve data from the API. But don't know how to Perform a CRUD operation in there. Can anybody help me with the simple codes on How to Send form data in JSON format to a Web Service in PHP/Any…
Deepak swain
  • 3,380
  • 1
  • 30
  • 26
28
votes
3 answers

Angular2 - Share data between components using services

I have an object that I want to share between my components into an Angular2 app. Here is the source of the first component: /* app.component.ts */ // ...imports import {ConfigService} from './config.service'; @Component({ selector: 'my-app', …
keversc
  • 346
  • 1
  • 3
  • 9
27
votes
5 answers

Difference between @Self and @Host Angular 2+ Dependency Injection Decorators

Kindly explain the difference between @Self and @Host. The angular API documentation gives some idea. But it's not clear to me. The example provided for Self uses ReflectiveInjector to exemplify usage. However, one would rarely, if ever, use…
27
votes
1 answer

Best practice for handling error in Angular2

Hi i am trying to receive error sent from catch block (service). in multiple components i need to show a popup with the error message displayed. please let me know how to create a generic method and call it inside service block. Like what i am…
Vaibhav
  • 771
  • 2
  • 11
  • 23
27
votes
1 answer

Angular2 - 'Can't resolve all parameters' error while injecting http into a custom service

I have built an ErrorHandlerLogger which is a service which extends ErrorHandler and logs error messages into a remote repository. ErrorHandlerLogger requires the Angular http client provided by the HttpModule. In the ErrorHandlerModule I import…
Picci
  • 16,775
  • 13
  • 70
  • 113
27
votes
2 answers

Angular 2 - multiple instance of service created

I have created AngularJS 2 service and use it in 2 differents components : App-Component & Sub-Component. Each one output property 'log' (a string) of my service. StateService class : @Injectable () class StateService { public log : string; …
Philippe sillon
  • 1,572
  • 2
  • 14
  • 20
26
votes
4 answers

ObjectUnsubscribedError: object unsubscribed error when I am using ngx-progress in angular 2

I am using ngx-progressbar bar in Angular 2 application. When app loading first it is working fine. second time it is showing error. I referred few article like medium.com for subscribe object. I did't get clearly. I need to make progress bar every…
Mathankumar K
  • 621
  • 1
  • 6
  • 14
26
votes
6 answers

Testing - Can't resolve all parameters for (ClassName)

Context I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features. ApiService's constructor signature is: constructor(metaManager: MetaManager, connector: ApiConnectorService,…
26
votes
4 answers

Angular 2 HTTP "Cannot resolve all parameters for 'AppService'"

I tried to import the http provider into a service, but I'm getting the following error: Cannot resolve all parameters for 'AppService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that…
25
votes
5 answers

How to use Dependency Injection (DI) correctly in Angular2?

I have been trying to figure out how the (DI) Dependency Injection work in Angular2. I ran into lots of problem/issue every time when I tried to Inject a service/or class into my components. From different googled articles, I need to either use…
George Huang
  • 2,504
  • 5
  • 25
  • 47