angular-services

Angular Service singleton constructor called multiple times

我的未来我决定 提交于 2019-12-07 02:28:39
问题 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 containing TestService as provider and imported it into AppModule. core.module.ts: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { TestService } from '../test.service'; @NgModule({ imports: [

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

前提是你 提交于 2019-12-06 20:19:12
问题 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(): Promise<MenuItem[]> { return Promise.resolve(ITEMS); } } The test for this service is here: import { TestBed, async, inject } from '@angular/core/testing'; import { ItemListService } from './item-list.service'; import { MenuItem } from './../classes/menu-item';

Angular 6 service injection exception

风格不统一 提交于 2019-12-06 16:46:05
I started angular two days ago, i m trying to create a service that will do a get request over my spring boot rest end point and I wish to display the result in my angular app Here is what i have tried till now My Service: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { ITweet } from './itweet'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ReactiveTwitterService { constructor(private http_client: HttpClient, private tweetTag: string) { } spring_webflux_service_url = 'http://localhost:8081/search';

Angular - inject in other way than constructor?

徘徊边缘 提交于 2019-12-06 14:08:47
In Angular, I have a service that has few things injected through the constructor(...) . However that service is also at some place created by calling the constructor. Therefore, adding another service it depends on to the parameters would change the API. I'd like to avoid that. Is there a way to inject a service into another service without adding it to the constructor parameters? E.g. field injection? import {Inject, Injectable} from "@angular/core"; import { Http, Request, ConnectionBackend, RequestOptions, RequestOptionsArgs, Response, Headers, RequestMethod } from "@angular/http"; import

How to share data between components using service and observable?

十年热恋 提交于 2019-12-06 11:12:29
Hi I am new to angular 2+, I am trying to share data between two components but the second component is not retrieving the data from the service, it gets an empty object. Service - using rxjs BehaviorSubject to keep the object import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { Observable } from 'rxjs/Observable'; @Injectable() export class PostsService { response: any = {}; private messageResponse = new BehaviorSubject(this.response); currentResponse = this.messageResponse.asObservable(); constructor(private http: Http) { } // Get all posts from the API getAllPosts() { return

Why do angular service “private” fields not update?

房东的猫 提交于 2019-12-06 08:25:24
If I follow this particular practice of making factories: myApp.factory('myService', function () { var somevalue = 2; var myServiceApi = { theValue: somevalue, updatevalue: updateValue } return myServiceApi; function updateValue(newValue) { somevalue = newValue; } }); Each and every time the service is injected the value of somevalue is always initialized as 2, even though I have updated it earlier with the UpdateValue method. if I however use a getter method on the value it is update in all instances of the service. http://jsfiddle.net/IngoVals/hd1r1bmp/ What is going on in the background

Unknown Provider when adding service to controller

穿精又带淫゛_ 提交于 2019-12-06 07:37:14
I created a Modal service and when I injected the service into the controller, I am receiving an error that says "$Injector: unpr Unknown Provider". Here is my code below. Let me know if I am missing something. This is the service I have so far. 'use strict'; angular.module('myApp.services', []) .factory('modalService', ['$scope', function($scope) { return { openMenuModal: function(templateLink, windowAnimation) { var modalInstance = $modal.open({ templateUrl: templateLink, backdrop: 'static', windowClass: windowAnimation, controller: function($scope, $modalInstance) { $scope.close = function(

How to use custom Cordova plugin methods in Angular/Ionic service

我们两清 提交于 2019-12-06 04:18:49
问题 I'm working on integrating a custom Cordova plugin into an Ionic app. We had a 3rd party create a Cordova plugin to interface with a bluetooth device. Running cordova platform ls shows the plugin has been correctly installed: $ cordova plugin ls > com.sitename.product 0.0.0 "DeviceProbe" The plugin contains a probe.js file containing methods for connecting, reading, polling and other actions. /plugins/com.sitename.product/www/probe.js var callNative = function(service, action, success, error,

Angular, show loading when any resource is in pending

怎甘沉沦 提交于 2019-12-06 03:43:04
问题 I already write a code to display a loader div, when any resources is in pending, no matter it's getting via $http.get or routing \ ng-view. I wan't only information if i'm going bad... flowHandler service: app.service('flowHandler', function(){ var count = 0; this.init = function() { count++ }; this.end = function() { count-- }; this.take = function() { return count }; }); The MainCTRL append into <body ng-controller="MainCTRL"> app.controller("MainCTRL", function($scope, flowHandler){ var

Angular Service singleton constructor called multiple times

Deadly 提交于 2019-12-05 07:10:43
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 containing TestService as provider and imported it into AppModule. core.module.ts: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { TestService } from '../test.service'; @NgModule({ imports: [ CommonModule ], declarations: [], providers: [ TestService ] }) export class CoreModule { } test.service.ts: