How to receive blob responses using Angular’s 2+ @angular/http module?

With the release of Angular2 final we can define for example a service: @Injectable() export class AngularService { constructor(private http: Http) {} download(model: MyModel) { //get file from service this.http.post(“http://localhost/a2/pdf.php”, JSON.stringify(model), { method: RequestMethod.Post, responseType: ResponseContentType.Blob, headers: new Headers({‘Content-Type’, ‘application/x-www-form-urlencoded’}) }).subscribe( response => { // download file var blob = new Blob([response.blob()], {type: ‘application/pdf’}); var … Read more

How to return value inside subscribe Angular 4

You can’t directly return totalQuestions like that, you need to use a subject to achieve that. getTotalQuestions(idForm:string): Observable<string> { let totalQuestions:number; var subject = new Subject<string>(); this.getFirebaseData(idForm+”/Metadatos”) .subscribe(items => { items.map(item => { totalQuestions=item.Total; console.log(totalQuestions); subject.next(totalQuestions); }); } ); return subject.asObservable(); } Usage: getTotalQuestion(idForm).subscribe((r)=>console.log(r))

How to return observable from subscribe

You can’t return an observable from subscribe but if you use map instead of subscribe then an Observable is returned. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> { // get route to be activated this.routeToActivate = route.routeConfig.path; // get user access levels return this._firebase.isUserAdminObservable .map(user => { // do something here // user.access_level; return true; }) .first(); // … Read more

Property ‘catch’ does not exist on type ‘Observable’

Warning: This solution is deprecated since Angular 5.5, please refer to Trent’s answer below ===================== Yes, you need to import the operator: import ‘rxjs/add/operator/catch’; Or import Observable this way: import {Observable} from ‘rxjs/Rx’; But in this case, you import all operators. See this question for more details: Angular HTTP GET with TypeScript error http.get(…).map is … Read more

Upgrading to Angular 10 – Fix CommonJS or AMD dependencies can cause optimization bailouts

When you use a dependency that is packaged with CommonJS, it can result in larger slower applications Starting with version 10, Angular now warns you when your build pulls in one of these bundles. If you’ve started seeing these warnings for your dependencies, let your dependency know that you’d prefer an ECMAScript module (ESM) bundle. … Read more

How to create an Observable from static data similar to http one in Angular?

Perhaps you could try to use the of method of the Observable class: import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; public fetchModel(uuid: string = undefined): Observable<string> { if(!uuid) { return Observable.of(new TestModel()).map(o => JSON.stringify(o)); } else { return this.http.get(“http://localhost:8080/myapp/api/model/” + uuid) .map(res => res.text()); } }

What is pipe for in RxJS?

The “pipeable” (former “lettable”) operators is the current and recommended way of using operators since RxJS 5.5. I strongly recommend you to read the official documentation on pipeable operators The main difference is that it’s easier to make custom operators and that it’s better treeshakable while not altering some global Observable object that could possible … Read more