angular2-services

Angular service call function in component

巧了我就是萌 提交于 2020-01-09 10:57:15
问题 So I know you can have two unrelated components communicate with each other via a service by having one component emit an event in the service and the other subscribe to it in the service. My question: Can a service call a function in a component directly? 回答1: Not by default. A service is an instance of a class, nothing more. @Injectable() class MyService { } @Component({ selector: 'my-component', ... )} class MyComponent { constructor(private myService:MyService) {} } @NgModule({ providers:

Why we should use RxJs of() function?

[亡魂溺海] 提交于 2020-01-09 07:13:11
问题 in service section of angular.io tutorial for angular2 I hit a method named of.for example : getHeroes(): Observable<Hero[]> { return of(HEROES); } or in below sample: getHero(id: number): Observable<Hero> { // Todo: send the message _after_ fetching the hero this.messageService.add(`HeroService: fetched hero id=${id}`); return of(HEROES.find(hero => hero.id === id)); } in angular.io Just explained used RxJS of() to return an Observable of mock heroes (Observable). and It was not explained

How to download pdf file in angular 2

一曲冷凌霜 提交于 2020-01-09 05:31:08
问题 This is function to download pdf file in angular 2 when I am downloading Pdf file it is corrupted file. So how do I solve this problem. It is displaying corrupted data like this. %PDF-1.5 %���� ↵66 0 obj <> endobj downloadFile() { //this.http.get('https://contactsapi.apispark.net/v1/companies/').subscribe( this.http.get('assets/Files/booking.pdf').subscribe( (response: any) => { console.log(response); let parsedResponse =(response) var blob = new Blob([response], {type: 'application/pdf'});

Inject service inside a service inside a service in my Angular 2 application

半城伤御伤魂 提交于 2020-01-04 07:10:11
问题 Update: I overlooked a service, the problem actually is about a service that injects a service that injects a service. I updated the post. I am having a hard time injecting a service inside a service in my Angular 2 application, I am not sure if I am interpreting some concepts of dependency injection wrong. My situation is as follows: I have a parentcomponent called app.component that uses a loginservice to authenticate users, this service is bootstraped in the app bootstrapping process. I

Angular2 Error when using angular2-img-cropper in Visual Studio Type FileReader is not assignable to type FileReader

孤街浪徒 提交于 2020-01-03 04:48:12
问题 I just tried to explore plugin angular2-img-cropper (https://github.com/cstefanache/angular2-img-cropper) and added some of the sample code from the plunker https://embed.plnkr.co/V91mKCNkBQZB5QO2MUP4/ to my app. But when I try to compile my app in Visual Studio 2015, its giving below error: Type FileReader is not assignable to type FileReader. Property onloadend is missing in type FileReader The actual error is in the below piece of code from above plunker: fileChangeListener($event) { var

Argument of type '{header:Header;}' is not assignable to parameter of type 'RequestOptionsArgs'

我怕爱的太早我们不能终老 提交于 2020-01-01 05:22:06
问题 Im Getting following error while trying to pass header in HTTP.POST/GET request Argument of type '{header:Header;}' is not assignable to parameter of type 'RequestOptionsArgs'. Property 'null' is missing in the type '{header:Header;}' I have try many solutions but no lock on it. here my code : import { Injectable } from '@angular/core'; import { Response, RequestOptions, Headers, Http, URLSearchParams} from '@angular/http'; import { Observable, Subject } from 'rxjs/Rx'; import {Company} from

Difference between EventEmitter.next() and EventEmitter.emit() in Angular 2

时光总嘲笑我的痴心妄想 提交于 2019-12-31 10:05:28
问题 What is the difference between EventEmitter.emit() and EventEmitter.next() ? Both dispatching the event to the subscribed listeners. export class MyService { @Output() someEvent$: EventEmitter<any> = new EventEmitter(); someFunc() { this.someEvent$.emit({myObj: true}); this.someEvent$.next({myObj: true}); } } The documenation for the EventEmitter is not so helpful at the moment. 回答1: They do the same. emit() is the current version, next() is deprecated. See also https://github.com/angular

How to handle the return value of async function in typescript? [duplicate]

耗尽温柔 提交于 2019-12-31 05:19:10
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 2 years ago . The casesService function handles a HTTP req and response and returns a single object. I want to return the object. But since it is an async functionality it returns empty object(this.caseBook). I want it to return the object only after it has value. public initData (selectedCaseId: number): CaseBook { this.casesService .GetCaseById(selectedCaseId) .subscribe(data

Hitting the http get request for linkedin giving error in angular 2

风格不统一 提交于 2019-12-31 03:02:31
问题 I am doing signup with linkedin and need to fetch access code and basic profile details. But when I post the request, I am getting CORS error in my console, when I hit the URL directly in browser, it takes me to the signin page correctly. What would be the problem hitting the request? CORS issue resolved: I found localhost:4200 does not had '/' and after including it , CORS error was resolved , but hitting my http.get is having a problem component: import { Injectable } from '@angular/core';

catch is not working for http.get Angular2

只愿长相守 提交于 2019-12-30 08:32:10
问题 I am trying to make an ajax call with Http service, which is working fine. Now I would like to handle failure cases. Example if we get 404 or any other errors. I am using the following code. import {Component,Inject} from '@angular/core'; import {Http, Response,HTTP_PROVIDERS} from '@angular/http'; import {DoService} from './service'; import 'rxjs/Rx'; import 'rxjs/add/operator/catch'; @Component({ selector: 'comp-one', template: `<h2>My First Angular 2 App</h2>{{data}} <input type="button"