angular2-services

Angular 2 Binding to a function on the view results to infinite calls to the data service

时光毁灭记忆、已成空白 提交于 2019-11-29 11:08:11
I'm trying to bind a src attribute of images inside an ngFor directive that looks like this: <div *ngFor="imageId of imageIds"> <img [attr.src]="imageSrc(imageId)" alt="{{imageId}}"> </div> the imageSrc method inside the component looks like this: imageSrc(imageId: string){ var hostUrl = "http://192.168.0.101:3000/"; var imageUrl = hostUrl+"images/"+imageId; var imgSrc = ""; this._imageService.getImage(imageId) .subscribe(image => { imgSrc = hostUrl + image.url }); return imgSrc; } The getImage function in the Injectable ImageService looks like this: getImage(id: string) { var _imageUrl =

Web workers in Angular 2 Dart

。_饼干妹妹 提交于 2019-11-29 10:57:38
问题 I'm experimenting with angular 2 for a large project that would benefit from handing off tasks to web workers. I've found examples of ng2 web workers for JavaScript and TypeScript but struggling to convert these into dart equivalent. Has anyone done this? Or know how to do this? Below is my current main.dart bootstrap file, the AppComponent should have access to the UI, and the CustomerService work in a worker. import 'package:angular2/platform/browser.dart'; import 'package:angular2/web

Make a service variable global in Angular 2

做~自己de王妃 提交于 2019-11-29 09:49:55
问题 How can I make a variable in a service in Angular 2 as a global variable so that two different components can access and modify it? public myMap : Map<string, string> = new Map<string, string>(); This is the map in the service and I'm populating it initially using a component. Now after some time in the application, I need to access the same populated map using a different component. How can I do this? When I access the map using another component, it displays as undefined. 回答1: Have a shared

Is it possible to use HostListener in a Service?

醉酒当歌 提交于 2019-11-29 09:07:06
I want to create a service which detects all keyboard input, translates the key strokes into actions based on a configurable mapping, and exposes observables which various elements can bind to to react to specific key presses. The following is a simplification of my code so far, it worked when HostListener was in a component, but now I've moved it into a service it never fires even though it is definitely initialised. Is it not possible to detect input like this in a service? import { Injectable, HostListener } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable() export

Angular 2 How to make singleton service available to lazy loaded modules

烂漫一生 提交于 2019-11-29 06:47:43
According to my understanding of Angular 2 rc5 , to make a service from another module (not AppModule ) available as a singleton to every component, even those lazy-loaded, we don't include the service in the providers array of that other module. We instead export it with RouterModule.forRoot() and import the result in AppModule According to the docs : The SharedModule should only provide the UserService when imported by the root AppModule. The SharedModule.forRoot method helps us meet this challenge...the SharedModule does not have providers ...When we add the SharedModule to the imports of

How I add Headers to http.get or http.post in Typescript and angular 2?

狂风中的少年 提交于 2019-11-29 05:43:21
getHeroes (): Observable<Heros[]> { return this.http.get(this.heroesUrl) .map(this.extractData) .catch(this.handleError); } Where I add the headers and how? looking for a simple example. HuntedCodes You can define a Headers object with a dictionary of HTTP key/value pairs, and then pass it in as an argument to http.get() and http.post() like this: const headerDict = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Access-Control-Allow-Headers': 'Content-Type', } const requestOptions = { headers: new Headers(headerDict), }; return this.http.get(this.heroesUrl, requestOptions

Resetting Angular 2 App

大兔子大兔子 提交于 2019-11-29 04:29:21
My Angular 2 app has a logout feature. We want to avoid doing a page reload if we can (i.e. document.location.href = '/'; ), but the logout process needs to reset the app so when another user logs in there's no residual data from the previous session. Here's our main.ts file: import 'es6-shim/es6-shim'; import './polyfills'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { ComponentRef, enableProdMode } from '@angular/core'; import { environment } from '@environment'; import { AppModule } from './app/app.module'; if (environment.production === true) {

property 'forkJoin' does not exist on type 'typeof observable' - angular2

被刻印的时光 ゝ 提交于 2019-11-29 02:57:06
I was looking to experiment with a forkJoin mainly using the accepted answer here: Angular2 Observable.forkJoin of observable variables - ReferenceError: Observable is not defined I'm getting the above error message as forkJoin isn't available. Anyone know why? Angular 6 changes this up a bit. forkJoin has been converted to a regular function so, instead of: import {Observable} from 'rxjs/Observable'; ... return Observable.forkJoin( this.http.get('someurl'), this.http.get('someotherurl')); Use: import {forkJoin} from 'rxjs'; ... return forkJoin( this.http.get('someurl'), this.http.get(

Angular 2 Component listen to change in service

做~自己de王妃 提交于 2019-11-28 22:58:40
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! Depending on how that boolean changes you could expose it as an Observable<boolean> on your service, and then subscribe to that stream in your component. Your service would look something like: @Injectable() export class MyBooleanService { myBool$: Observable<boolean>; private boolSubject: Subject<boolean>; constructor() { this.boolSubject = new Subject<boolean>(); this.myBool$

Subscribe to observable is returning undefined

為{幸葍}努か 提交于 2019-11-28 22:05:46
So I am trying to subscribe to a simple service that return data from a local JSON file. I have managed to get the service working, I can log it out in the function, but when I subscribe to the service in the angular 2 component, it is always undefined. I'm not sure why? Any help would be much appreciated. API service export class ApiService { public data: any; constructor(private _http: Http) { } getData(): any { return this._http.get('api.json').map((response: Response) => { console.log('in response', response.json()); //This logs the Object this.data = response.json(); return this.data; })