angular-services

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

你。 提交于 2020-05-25 09:34:30
问题 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({ declarations: [...] providers: [ AppConfig, { provide: APP_INITIALIZER, useFactory: initializeConfig, deps: [AppConfig], multi: true } ] }) export class AppModule { } app.config.ts @Injectable() export class AppConfig { config: any; constructor( private

How to make a synchronous call in angular 5?

蹲街弑〆低调 提交于 2020-04-10 09:17:27
问题 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<ResponseData> { return this.http.get<ResponseData>(ApplicationURLS.GetCurrentUserInformation) .map(response => { return response; }); //.catch(error => this.handleError(error)); } This is my component: public GetCurrentUserInformation(): any { return this.loginService.GetCurrentUserData().subscribe

@Injectable() decorator and providers array

三世轮回 提交于 2020-03-21 06:50:11
问题 Does a service that is provided in "root" within the @Injectable() decorator still have to be in the providers array of the module? The Angular documentation doesn't really give me an answer or I don't quite understand it. Inside my core folder I have a authentication service which is provided in root. I wan't to import my core module inside the app module in order to use all the services and components provided. Do I have to additionally set up the service in the providers array of the

What is the difference between providedIn any and root

随声附和 提交于 2020-02-28 09:51:49
问题 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 { } 回答1: The difference between the root and any as per offical documentation : root : The application-level injector in most apps. platform : A special singleton platform injector shared by all applications on the page. any : The NgModule injector

What is the difference between providedIn any and root

你。 提交于 2020-02-28 09:51:27
问题 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 { } 回答1: The difference between the root and any as per offical documentation : root : The application-level injector in most apps. platform : A special singleton platform injector shared by all applications on the page. any : The NgModule injector

What is the difference between providedIn any and root

跟風遠走 提交于 2020-02-28 09:51:12
问题 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 { } 回答1: The difference between the root and any as per offical documentation : root : The application-level injector in most apps. platform : A special singleton platform injector shared by all applications on the page. any : The NgModule injector

When to use ngrx/effect in angular2

那年仲夏 提交于 2020-01-22 09:47:24
问题 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 can be used upon the api requests. And here my question comes, why should I use the ngrx/effect library, over just calling the corresponding function in my service from my container component to perform the api request and on success dispatch

Angular - Updating component value, when service value changes

北城以北 提交于 2020-01-17 01:25:06
问题 I have multiple components (in an NgFor loop) that call the same service. What I would like is that when one component changes a value in that same service, that new value then gets sent to every component that calls that service and subsequently updates that value in a component variable. I hope that makes sense. If you need some more information, please let me know. 回答1: Instead of a BehaviorSubject (which are often overkill in simple scenarios), you can use simple getters: Service has some

How to use a Web Worker in AngularJS?

纵饮孤独 提交于 2020-01-14 08:16:29
问题 I'm using AngularJS Seed and I want to see a working implementation of a Web Worker. I want to make a simple Web Worker work in order to understand it, but I'm running into an issue with the functionality. I have the Web Worker code in the services.js like so: 'use strict'; /* Services */ var app = angular.module('myApp.services', []). app.factory("HelloWorldService",['$q',function($q){ var worker = new Worker('js/doWork.js'); var defer; worker.addEventListener('message', function(e) {

Can't access my service in my controller (undefined)

久未见 提交于 2020-01-14 03:42:13
问题 I am trying to create a service to do some AJAX requests. However, my service keeps getting undefined when I try to do something with it in my controllers. Here is my code. I have found a lot of examples on here, and I've tried to follow a lot of them but no matter what I do my service keeps getting undefined. My service: MyApp.factory('myService', function($http) { return { findAll: function() { return $http.get('/findAll') .then(function(result) { return result.data; }); } } }); My