angular-services

Angular 5 download excel file with post request

China☆狼群 提交于 2019-12-05 03:31:11
I am facing an issue where I have downloaded an Excel file with Angular 1 but if I am implementing the same code in Angular 5 it is showing the error that your file is corrupted. My response is in ArrayBuffer and I am unable to read the file. Below is my code: Service: DownloadData(model:requiredParams):Observable<any>{ const headers = new Headers(); const requestOptions = new RequestOptions({ headers: headers }); requestOptions.headers.append('Content-Type', 'application/json'); const body = JSON.stringify(model); return this.http.post(url, body, requestOptions) .map((res:any) => res) .catch(

Invalid provider for the NgModule 'DynamicTestModule' when testing a service in Angular 2

百般思念 提交于 2019-12-05 03:06:39
I have the following service: import { Injectable } from '@angular/core'; import { MenuItem } from './../classes/menu-item'; import { ITEMS } from './../static-data/items-list'; @Injectable() export class ItemsListService { getItems(): Promise<MenuItem[]> { return Promise.resolve(ITEMS); } } The test for this service is here: import { TestBed, async, inject } from '@angular/core/testing'; import { ItemListService } from './item-list.service'; import { MenuItem } from './../classes/menu-item'; import { ITEMS } from './../static-data/items-list'; describe('ItemListService', () => { beforeEach(()

Redirect to Previous Page In Angular

时光毁灭记忆、已成空白 提交于 2019-12-04 20:00:46
I have installed a package called ngx-pagination. But i have a problem since when you navigate away from the previous page and you click the back button, it redirects you to the first page instead of going back to the previous page? How can i make it return to the previous page if i click the back button in the browser? Here's my code below. TS config: any; constructor() { this.config = { currentPage: 1, itemsPerPage: 2 }; this.route.paramMap .map(params => params.get('page')) .subscribe(page => this.config.currentPage = page); } } ngOnInit() { this.getAllBooks(); } pageChange(newPage: number)

Angular, show loading when any resource is in pending

孤者浪人 提交于 2019-12-04 08:11:46
I already write a code to display a loader div, when any resources is in pending, no matter it's getting via $http.get or routing \ ng-view. I wan't only information if i'm going bad... flowHandler service: app.service('flowHandler', function(){ var count = 0; this.init = function() { count++ }; this.end = function() { count-- }; this.take = function() { return count }; }); The MainCTRL append into <body ng-controller="MainCTRL"> app.controller("MainCTRL", function($scope, flowHandler){ var _this = this; $scope.pageTitle = "MainCTRL"; $scope.menu = []; $scope.loader = flowHandler.take();

Angular 2: Two backend service calls on success of first service

可紊 提交于 2019-12-04 05:18:23
问题 In my Angular 2 app I have backend service as below. getUserInterests() { return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json()); } After calling this service I want to call another service on success of previous one. 2nd service let params: URLSearchParams = new URLSearchParams(); params.set('access_token', localStorage.getItem('access_token')); return this.http.get('http://localhost:8080/user/selections', { search: params }).map((res: Response) =>

How to keep globally current user until logout with angular_devise?

ぐ巨炮叔叔 提交于 2019-12-03 22:36:16
How to create own service accessible globally that will call server by currentUser() only once on page load, if User is logged in then keep it and provide data to controllers or states until logout? Now I'm resolving many times in many states or controllers currentUser() , I found in docs: authparseresponse that it is possibility to make own service, but I don't know how to handle this in right way. e.g in ui-router I've two server calls, would be sufficient only when page is loaded: .state('login', { url: '/login', templateUrl: 'auth/_login.html', controller: 'AuthCtrl', onEnter: [ '$state',

Send an uploaded image to the server and save it in the server

一曲冷凌霜 提交于 2019-12-03 19:58:23
问题 I want to upload an image and save it in the server. I uploaded the image an got the preview too, but I am stuck in sending that image to the server. I want to send this image to the server using angular services. This is the html code <input type="file" fileread="vm.uploadme" /> <img src="{{vm.uploadme}}" width="100" height="50" alt="Image preview..."> This is the directive (function(){ angular.module('appBuilderApp').directive("fileread", [function () { return { scope: { fileread: "=" },

What is the lifecycle of a service in Angular 5

[亡魂溺海] 提交于 2019-12-03 10:53:19
问题 Angular 5 When is a service created and destroyed, what are its lifecycle hooks (if any) and how is its data shared between components? EDIT : To clarify, this is NOT a question about the lifecycle of components. This question is about the lifecycle of services. In case a service does not have a lifecycle, how is the flow of data between components and services managed? 回答1: Service can have 2 scopes. If service is declared on your module, you have same instance shared for all, this means

Angular v4: Do we store data in a Service or the Component or both?

二次信任 提交于 2019-12-03 07:45:56
问题 Angular v4: Do we store data in a Service or the Component or both? After reviewing quite a few tutorials, as well as reading the documentation of Angular, I'm still not clear on this subject. https://angular.io/tutorial/toh-pt2 Angular's tutorial clearly shows data stored in the Component. https://angular.io/guide/architecture#services Angular's Architecture > Services section shows code with the service having an array of data (is this proper?). If we store data in Components , we would

Angular Search for value in JSON and display corresponding data

隐身守侯 提交于 2019-12-03 03:23:12
问题 What i am trying to do is simple. A user enters a value, on button click, my JS calls a service to retreive my JSON data and perform a search on the value entered against the JSON and if a match is found, display the 'Owner'. HTML: <div ng-app="myApp"> <div ng-controller="MainCtrl"> <input type="text" ng-model="enteredValue"> </br> <button type="button" ng-Click="findValue(enteredValue)">Search</button> </div> </div> JS: angular.module('myApp', []).controller('MainCtrl', function ($scope,