angular2-services

Promise and subscribe

╄→尐↘猪︶ㄣ 提交于 2019-12-19 02:40:28
问题 I have an Angular2 (ionic2) application. I have a function that request cities but I get an error that Property subscribe does not exists on this.cityService.getAllCities() . cityPage.ts has a function like this: getCities(){ this.cityService.getAllCities() .subscribe(cityData => { this.cityList = cityData; }, err => console.log(err), () => console.log('Complete!') ); } my cityService.getAllCities() function looks like this: getAllCities(){ return new Promise (resolve => { this.storage.ready(

Angular 2. How to handle 4xx errors with redirect in Observable?

China☆狼群 提交于 2019-12-18 16:45:12
问题 I have a service that calls an api getItems(itemId: number): Observable<any> { return this.http.get(url, headers) .map(this.extractDataSingle) .catch(this.handleError) } If the server responds with an 4xx the catch part is called. Here is my handleError method. private handleError = (error: any) => { //Here I want to redirect to login. } I want to redirect to the login page. Simply typing this._router.navigate(['Login']); does not work since I have to return a Observable . Returning an empty

Type 'ElementRef' is not generic

醉酒当歌 提交于 2019-12-18 14:53:40
问题 I am working with a project using Material Component in Angular 5. I did update my Visual code but I don't know what happened. I am facing an issue "Type 'ElementRef' is not generic." I have been stuck on this issue since morning. Error in this line _inputElement: ElementRef<HTMLInputElement>; constructor(toggleGroup: MatButtonToggleGroup, _changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef<HTMLInputElement>, _focusMonitor: FocusMonitor); 回答1: I think the problem could be if your

Angular 2 useExisting providers

家住魔仙堡 提交于 2019-12-18 14:41:49
问题 What are the usages for useExisting provider? Is it useExistingOrThrowIfThereIsNone or useExistingOrCreateIfThereIsNone ? Can one of these behaviours be chosen on purpose somehow, depending on our needs? If one of them is not supported, can an unsupported one be emulated? The documentation is totally unclear on that and just gives an example that useExisting can reuse an instance from useClass . 回答1: With this example providers: [ A, {provide: B, useClass: A}, {provide: C, useExisting: A}] If

How to detect variable change in Angular2

廉价感情. 提交于 2019-12-18 11:55:56
问题 I have the following config object which is set before the constructor is running: config: Object = { onSlideChangeEnd : function(slide:any) { this.currentSlideIndex = slide.activeIndex; } }; I want to notify a service about the change, the problem is that the service is not available yet when the configuration is set: constructor(private user: UserService, private layout: LayoutService) { } How can I notify the service on this variable change? 回答1: Well, as suggested use Observables, this is

Angular 2 animate *ngFor list item one after other using new Animation support in RC 5

故事扮演 提交于 2019-12-18 10:35:55
问题 I have a component that fetches list of items from server and then display that list using *ngFor in template. I want the list to be displayed with some animation, but one after other. I mean each list item should animate in after other. I am trying something like this: import { Component, Input, trigger, state, animate, transition, style } from '@angular/core'; @Component({ selector: 'list-item', template: ` <li @flyInOut="'in'">{{item}}</li>`, animations: [ trigger('flyInOut', [ state('in',

How can I detect service variable change when updated from another component?

…衆ロ難τιáo~ 提交于 2019-12-18 10:20:04
问题 I'm trying to get the updated value from a service variable ( isSidebarVisible ) which is keeps on updated by another component ( header ) with a click event ( toggleSidebar ). sidebar.service.ts import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable() export class SidebarService { isSidebarVisible: boolean; sidebarVisibilityChange: Subject<boolean> = new Subject<boolean>(); constructor() { this.isSidebarVisible = false; } toggleSidebarVisibilty() {

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 04:36:13
问题 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. 回答1: 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

angularfire2 best way to increment a value?

可紊 提交于 2019-12-18 04:17:13
问题 I searched in many forums, questions, in doc but can't find the correct solution. Problem What is the best way to increment a value using angularfire2 ? I saw we could use [transaction()][] but it's not for angularfire2 actually. Or with snapshot ? user.service.ts incrementLike(userToIncrementLike){ this.af.database.object('users/' + userToIncrementLike.uid).subscribe((userObject) => { var newUser = { likes: userObject.likes + 1 }; }); this.af.database.object('users/' + userToIncrementLike

Inject all Services that implement some Interface

℡╲_俬逩灬. 提交于 2019-12-18 04:01:28
问题 Simple scenario: I have multiple Services that implement a common Interface. All those Services are registered within the bootstrap method. Now I'd like to have another Service, which injects all registered Services that implement the common Interface. i.e. export interface MyInterface { foo(): void; } export class Service1 implements MyInterface { foo() { console.out("bar"); } } export class Service2 implements MyInterface { foo() { console.out("baz"); } } export class CollectorService {