angular2-services

Emit and broadcast events throughout application in Angular

大兔子大兔子 提交于 2019-12-12 09:58:43
问题 Is there a way to emit event in angular2 that can be listened to in entire application? Like we had in AngularJS using $rootScope.broadcast and emit . Can this same thing be achieved in angular2? I read about @Output() and EventEmitter() and implemented it but it restricts only the parent to listen to the event emmitted by child. I read about BehaviorSubject being one of the ways to do this. Is that the right approach? Any other solution for this? 回答1: In Angular2 there is no global scope.

Cookie undefined in service | cookie value exist in component | angular

不羁的心 提交于 2019-12-12 06:57:57
问题 api.service.ts import { Injectable, Inject } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/throw'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/timeout'; import 'rxjs/add/operator/retry'; import { CacheService } from './cache.service'; import { AuthService } from '../services/auth.service'; import { CookieService } from 'angular2-cookie/core';

Angular2,Ionic2: How to create a countdown timer with hours,mins,secs and millisecs?

半世苍凉 提交于 2019-12-12 06:46:05
问题 I want to create a countdown timer which has 4 fields hours:minutes:seconds:milliseconds. I've gone through the following links before I posted this question.I don't know why should we have a hardcoded date and then subtract something from it. I'm not able to get the concept of Observable here. Time CountDown in angular 2 Angular 2 - Countdown timer Can someone please help me? This is what I've written from https://stackoverflow.com/a/40784539/4579897 private eventDate: Date = new Date(2018,

Notify component when service function is complete in angular

柔情痞子 提交于 2019-12-12 06:18:12
问题 I writing a service which do a http query to the backend: getPlacesForUser(){ this.http.get("http://localhost:8080/processPlaces") .map(res => res.text()) .subscribe( data => this.result = data, err => this.logError(err), () => console.log('Places Request completed') ) } Now I want to update the components-side, if the request is completed: processPlaces(){ this._backendService.getPlacesForUser(); } How can I communicate between the component and service? Furthermore I searching for the best

angular2 is it possible to share observable data with other components?

こ雲淡風輕ζ 提交于 2019-12-12 05:14:19
问题 I have a service that GETS a user via a RESTful api. It uses the Observable implementation. I also have a parent component with 8 child components. I want to know if and how I can make a single HTTP request in the parent component, to get the user and share it amongst its children components. Here's the call to define user in the parent: this.userApi.getUser(id) .subscribe(response => {this.user = response;}, error => error); I've imported and injected the user model into the constructor() of

ViewChild - Angular 2 Javascript

孤人 提交于 2019-12-12 04:59:26
问题 How can I use ViewChild in Angular 2 Javascript? I have referred angular.io docs and I have tried with the following code and it is not working. Can anyone help me? Thanks in advance. main.html <router-outlet></router-outlet> app.component.js (function (app) { app.AppComponent = ng.core .Component({ selector: 'my-app', templateUrl: 'app/views/main.html', directives: [ng.router.ROUTER_DIRECTIVES], //<----not loading components here viewProviders: [ng.http.HTTP_PROVIDERS], queries: {

Communication between multiple components with a service by using Observable and Subject in Angular 2

你。 提交于 2019-12-12 04:45:38
问题 I am new to rxjs, so I would like to ask a question regarding Angular 2 , Observables and BehaviorSubject/Subject . So, what I want to achieve is : onclick of a button inside a ComponentA to notify other components for example ComponentB , ComponentC . What I did so far is to create a service : private isMenuOpenBS = new BehaviorSubject(false); toggleMenu(): void { this.isMenuOpenBS.next(!this.isMenuOpenBS.getValue()); } getMenuState(): Observable<boolean> { return this.isMenuOpenBS

How do I return an array instead of a FirebaseListObservable

瘦欲@ 提交于 2019-12-12 04:21:43
问题 I have ng2-charts working on my Angular-cli built application. I'm pretty new, so any help is appreciated. I have data setup on Firebase. I create a method in a service to access that data and when I use this code getGraphData():Observable<any>{ var graphData$ = this.af.database.list(`graphdata/${this.uid}`) .map(tspa => tspa.map(tpa => tpa.$value)) .do(console.log); graphData$.subscribe(); return Observable.of([]); } the console logs the correct data. ex. [1000, 5000, 2000] the problem is

Request data once through parent component and make accessible to all child routes? (Angular2)

别来无恙 提交于 2019-12-12 04:13:38
问题 I have a parent component with two child routes. Each child route will need to use/access the same data retrieved from a GET request. I think it may be wasteful to query the server each time a child route is loaded, so I thought a good way of doing this would be to query the server once through the parent route and then sharing the data afterwards through a bi-directional service, which I'm trying to implement following the angular docs closely. However, neither of the child components are

angular2 services returns undefined

拟墨画扇 提交于 2019-12-12 03:48:36
问题 I cannot get the reason why one of my services always returns undefined however network tab in developer mode shows me that data has been received. Could somebody help me to figure out where my mistake is? My component looks next way: export interface Sponsors { id; sponsorCode; sponsorName; active; clientId; countryId; cloudRegionId; } @Component({ selector: 'app-sponsors', templateUrl: './sponsors.component.html', styleUrls: ['./sponsors.component.css'], }) export class SponsorsComponent