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
18
votes
4 answers

How to send email using Angular 7

How do I create an email service in Angular 7 that contains a method for sending out emails? For example: // email.service.ts import { Injectable } from ‘@angular/core’; @Injectable() export class EmailService { constructor() { } //…
Mokka soru
  • 503
  • 2
  • 4
  • 12
18
votes
3 answers

Angular 2 Injected Service is undefined

Why is my service (this.loggerService) undefined in my DataHandlerService when it has been injected? I thought dependency injection took care of this. My loggerService works in other services. Please help me realize where I am going wrong. My…
LanceM
  • 1,888
  • 4
  • 23
  • 41
17
votes
2 answers

What does the app.module.ts file serve for, what should I do inside of it?

I'm getting used to Angular 2 but I have a few questions concerning the app.module.ts file. Why do I have to do the imports in this file since I will be doing the inputs again in the app.components.ts file. For example: I import my custom pipe…
masterach
  • 437
  • 1
  • 6
  • 19
17
votes
2 answers

Angular 2 send data from component to service

My target is to send data from Angular component to service and use service methods to work on it. Example: export class SomeComponent { public data: Array = MyData; public constructor(private myService: MyService) { …
17
votes
3 answers

Angular http returns $$state object

I have the following factory defined: angular.module("account").factory("users",["$http", function(a){ return { getUser: function(){ return a.get("/user/me").then(function(r){ return r.data; …
wdphd
  • 905
  • 6
  • 15
  • 25
16
votes
1 answer

Testing async (Promise) methods in Angular 2 Services

This is an interesting problem: I'm trying to test a Service, that uses the Ionic BarcodeScanner. I have a repo based on the ionic unit-testing repository in order to try testing. I'm mocking the BarcodeScanner.scan method via…
Sentenza
  • 1,269
  • 11
  • 24
16
votes
7 answers

How to make a synchronous call in angular 5?

So, I was trying to get the solution of this problem. But, somehow I am unable to do so, May be because of the lack of knowledge in angular 5. This is my service: GetCurrentUserData(): Observable { return…
Just code
  • 13,553
  • 10
  • 51
  • 93
16
votes
1 answer

Angular 2: Two backend service calls on success of first service

In my Angular 2 app I have backend service as below. getUserInterests() { return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json()); } After calling this service I want to call another service on success…
Rose18
  • 2,892
  • 8
  • 47
  • 98
16
votes
1 answer

angularjs $httpProvider interceptor documentation

I am new to angular (and programming), here is a seemingly simple question but I could not figure it out. some tutorials suggests using $httpProvider.interceptors.push('interceptorName') to manipulate the http request and response. I want to know…
webberpuma
  • 691
  • 1
  • 6
  • 21
15
votes
7 answers

IONIC 3 CORS ISSUE

I am having a CORS issue with Ionic 3 when attempting to make GET calls to an API. I am using Ionic local server, running ionic serve in the command line for live server. Error No 'Access-Control-Allow-Origin' header is present on the requested…
Chris Simmons
  • 249
  • 2
  • 7
  • 19
15
votes
4 answers

Make sure a service is instantiated

Background We're building an Angular2 app, and are accumulating a lot of specific services relating to one module. All these services are loosely coupled to a Subject event system in the app. Instantiation via the Constructor Because these…
Ben Winding
  • 10,208
  • 4
  • 80
  • 67
14
votes
3 answers

When to use ngrx/effect in angular2

I have an anuglar2 project that communicates with an api. Recently, I decided to integrate ngrx/store to maintain the state of the components, and follow the dump-smart component architecture. But then while moving on, I read about ngrx/effect which…
Salma Hamed
  • 2,000
  • 4
  • 26
  • 45
12
votes
2 answers

Generic type Subject requires 1 type argument(s). - Angular

I'm getting data from my service and I've red about it's important to unsubscribe after subscribe, and here is how I did it: export class RItemComponent implements OnInit { apiPath = environment.apiUrl; private ngUnsubscribe: Subject = new…
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
12
votes
1 answer

Angular 4.x: No provider for Service

I am having trouble injecting a service into another service in Angular 4.x and receive the error: Error: No provider for SkillsService! I have created a repo that reproduces this error. You can run this locally by cloning the repo and simply…
Baldy
  • 3,621
  • 4
  • 38
  • 60
11
votes
3 answers

How to dynamically create component instance with input/output from a service and inject it to DOM separately?

In creating dynamic components in Angular 2, I found out that this process requires ViewContainerRef in order to add newly created component to DOM. And in passing @Input and @Outputto those dynamically created components, I found the answer in the…
Dangular
  • 389
  • 1
  • 3
  • 15