angular2-services

Are Angular2's services supposed to be stateless?

六眼飞鱼酱① 提交于 2019-12-10 03:39:42
问题 I come from Java world where services are commonly meant to be stateless. Should be services in Angular2 also stateless? Or can we simply store the state, because we do not have to care about concurrent thread access as in Java example? At https://angular.io/docs/ts/latest/guide/router.html#!#teach-authguard-to-authenticate in AuthService class the state is stored. Is it just to simplify the example or is it a common practice? I know that services are instantiated and live for the scope where

How to reload the component of same URL in Angular 2?

心不动则不痛 提交于 2019-12-10 03:04:55
问题 I am trying to reload the same url in Angular 2 by using router.navigate but it is not working. url : http://localhost:3000/landing Scenario : I am on http://localhost:3000/landing and now I am changing a particular parameter in the page which should reload the page. Code : let link = ['Landing']; this.router.navigate(link); 回答1: Navigate to some dummy component and then navigate to the component that you want to reload. //the second parameter _skipLocationChange=true to avoid that url in

Angular 2 get routeParams in a service

冷暖自知 提交于 2019-12-09 17:37:10
问题 I want to shift the logic from component to service. But I found out that I can't get the routeParams in a service. My component looks like import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Params } from '@angular/router'; import { MyService } from '../services/my.service'; @Component({ moduleId: module.id, templateUrl: 'my.component.html', styleUrls: ['my.component.css'] }) export class MyComponent implements OnInit { constructor(private myService: MyService,

Get status code http.get response angular2

折月煮酒 提交于 2019-12-09 14:45:44
问题 I need to get the status code of the following http call and return it as a string //This method must return the status of the http response confirmEmail(mailToken):Observable<String>{ return this.http.get(this.baseUrl+"users/activate?mailToken="+mailToken) .map(this.extractData) .catch(this.handleError); } thx! 回答1: Adding answer for versions of Angular >= 4.3 (including 9) with new HttpClient that replaces http import {HttpClientModule} from '@angular/common/http'; // Notice it is imported

RxJs subscribers, passing null values?

扶醉桌前 提交于 2019-12-08 23:14:58
问题 I've spent the day diving into RxJS with Angular2, as the practice of modeling user interfaces as streams is new to me. I'm experimenting with a user service that provides a stream of User objects. The first User object will be provided when the user is authenticated. Additional User Objects may be provided when the User is updated, e.g. they update their profile. If the user is not logged when the application loads, or they logout, then null is emitted. As such, an observer implementation in

How to pass parameters for dependency while Injecting services in services in Angular 2 ( Ionic 2 / Angular 2 / Typescript )

佐手、 提交于 2019-12-08 22:05:37
问题 I'm making a sample application to make connection to a websocket server in ionic 2 in typescript. link to repo My requirement is to make the websocket connection during application start up I'm using angular2-websocket to creating the connection. References : http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html I'm getting a error " Cannot resolve all parameters for '

DomSanitizationService is not a function

北慕城南 提交于 2019-12-08 19:36:22
问题 As people always say there is no question called a dumb question. I am learning Angular 2.0 following the official tutorial now. It is using rc2.0 as I can see from the packageconfig file. I was trying to suppress the frame work complaining the "Unsafe" url in the iFrame tag. I have checked the instructions from this Stack Overflow Question and followed everything that's been shown in the LIVE Plunker. My VS Code doesn't complain anything in the compile time but from the Chrome inspector I

Property 'includes' is missing in type 'Subscription' -angular2

那年仲夏 提交于 2019-12-08 18:28:27
I have been trying to get an array of objects from node to a service onInit and assign it to component. I am able to view data in service but when i try to assign to a variable in component, I get below error. I have included my code as well. Please favour on this.All i need is to just take that array from service and assign it to finallist in component. ERROR in C:/Users/girija/Documents/animapp/src/app/components/list.component.ts (28,5): Type 'Subscription' is not assignable to type 'any[]'. Property 'includes' is missing in type 'Subscription'. My code is below: app.service.ts: public

Using index.ts file to export class causes undefined in injected constructor

孤街醉人 提交于 2019-12-08 15:55:47
问题 Im using an index.ts file to encapsulate exports as mentioned in the angular 2 style guide (https://github.com/mgechev/angular2-style-guide/blob/master/old/README.md#directory-structure). This worked fine across the app Im writing, but for some reason in one service that im trying to inject into another service this causes a strange Error. The exported class: import {Injectable} from "angular2/core"; @Injectable() export class UserIds{ private _signature_id:string; private _role_id:number;

How to update Data in 2 components using single instance of the Service Class in Angular 2

谁都会走 提交于 2019-12-08 14:05:06
问题 This is my service class in which i have one variable called " dataValue". import { Injectable } from '@angular/core'; @Injectable() export class DataService { constructor() { console.log("new instance"); } dataValue: string = "initial value"; } Here it is my first component in which i am getting and intializing the service variable with the variable i define in this component.and implementing two way data binding on component variable. import { Component } from '@angular/core'; import {