angular2-services

angular 2 inheriting from a base-component

自作多情 提交于 2019-12-03 13:23:33
问题 My question is an extension to another question here on so: Angular2 and class inheritance support And here is my plunckr: http://plnkr.co/edit/ihdAJuUcyOj5Ze93BwIQ?p=preview What I am trying to do is the following: I have some common functionallity whcih all of my components will have to use. As it already has been answered in the aforementioned question, this can be done. My question is: Can I have dependencies injected in the base-component? In my plunkr the declared dependency (

Difference between @Self and @Host Angular 2+ Dependency Injection Decorators

我是研究僧i 提交于 2019-12-03 12:53:17
问题 Kindly explain the difference between @Self and @Host. The angular API documentation gives some idea. But it's not clear to me. The example provided for Self uses ReflectiveInjector to exemplify usage. However, one would rarely, if ever, use ReflectiveInjector in actual app code (probably more in testing).. Can you give an example of where you would use @Self instead of @Host outside of such test scenarios?? 回答1: tl;dr It looks like when @Self is used, Angular will only look for a value that

Async initialization of Angular 2 service

旧时模样 提交于 2019-12-03 11:12:56
问题 I have an Angular 2 service that needs to do async work when it is initalized, and should not be usable before this initialization has been completed. @Injectable() export class Api { private user; private storage; constructor(private http: Http) { this.storage = LocalStorage; this.storage.get('user').then(json => { if (json !== "") { this.user = JSON.parse(json); } }); } // one of many methods public getSomethingFromServer() { // make a http request that depends on this.user } } As it

Angular2 - 'Can't resolve all parameters' error while injecting http into a custom service

坚强是说给别人听的谎言 提交于 2019-12-03 04:27:29
I have built an ErrorHandlerLogger which is a service which extends ErrorHandler and logs error messages into a remote repository. ErrorHandlerLogger requires the Angular http client provided by the HttpModule . In the ErrorHandlerModule I import HttpModule and define ErrorHandlerLogger as provider. In the AppModule I import ErrorHandlerModule . When I launch the app I get the following error message Uncaught Error: Can't resolve all parameters for ErrorHandlerLogger: (?). Here my code ErrorHandlerModule import { NgModule, ErrorHandler } from '@angular/core'; import { HttpModule } from '

angular 2 inheriting from a base-component

帅比萌擦擦* 提交于 2019-12-03 03:31:26
My question is an extension to another question here on so: Angular2 and class inheritance support And here is my plunckr: http://plnkr.co/edit/ihdAJuUcyOj5Ze93BwIQ?p=preview What I am trying to do is the following: I have some common functionallity whcih all of my components will have to use. As it already has been answered in the aforementioned question, this can be done. My question is: Can I have dependencies injected in the base-component? In my plunkr the declared dependency ( FormBuilder ) is undefined when logged to console. import {AfterContentChecked, Component, ContentChildren,

Difference between @Self and @Host Angular 2+ Dependency Injection Decorators

依然范特西╮ 提交于 2019-12-03 03:31:14
Kindly explain the difference between @Self and @Host . The angular API documentation gives some idea. But it's not clear to me. The example provided for Self uses ReflectiveInjector to exemplify usage. However, one would rarely, if ever, use ReflectiveInjector in actual app code (probably more in testing).. Can you give an example of where you would use @Self instead of @Host outside of such test scenarios?? tl;dr It looks like when @Self is used, Angular will only look for a value that is bound on the component injector for the element that this Directive/Component exists on. It looks like

Http Get Response Updates HTML but giving undefined error in console

 ̄綄美尐妖づ 提交于 2019-12-02 22:56:20
问题 When I am updating my html with the response Obj using HttpClient it updates the values but gives multiple errors. File Name - auth-service.ts. import { any } from 'codelyzer/util/function'; import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Injectable() export class AuthService { constructor(private httpClient: HttpClient) { } get() { return this.httpClient.get<any>('/capi/v2/users/me', { observe: 'body', responseType: 'json' }); } }; File Name -

Shared service in Angular2

岁酱吖の 提交于 2019-12-02 21:47:46
问题 I'm trying to make a shared service for my app. import { Injectable } from '@angular/core'; @Injectable() export class SharedService { testService() { console.log('share!'); } } Then I inject it in my app.component's providers but when I tried to call it in the constructor of a child component like this: constructor(public sharedService: SharedService) {} I've got an error: Can't resolve all parameters for MyComponent . I also tried to inject it in my app.module providers and also got this

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

拈花ヽ惹草 提交于 2019-12-02 21:32:19
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. They do the same. emit() is the current version, next() is deprecated. See also https://github.com/angular/angular/blob/b5b6ece65a96f5b8f134ad4899b56bf84afe3ba0/modules/angular2/src/facade/async.dart#L49 来源: https:/

Angular2: inject server side config into service

白昼怎懂夜的黑 提交于 2019-12-02 12:14:38
问题 I'm using Angular 2.0.0 with TypeScript in ASP.NET Core. My goal is to create AppConfig service in my app, based on server-side variables. With a help from few other answers, I was able to create following code: Index.cshtml <app> <i class="fa fa-spin fa-5x fa-spinner"></i> </app> <script> System.import('/app/main').then((m) => { var config = { apiUrl: @options.ApiServerUrl }; m.RunApplication(config); }, console.error.bind(console)); </script> app.config.ts import { Injectable } from "