How to access service in export function restangularInit
See original GitHub issueI have a service (AuthService) that I need to access in my restangularInit function (in my app.module.ts) but I dont know how I cant get access to it.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MaterialModule } from '@angular/material';
import { FlexLayoutModule } from "@angular/flex-layout";
import { RestangularModule } from 'ng2-restangular';
// Components
import { AppComponent } from './app.component';
import { ProductComponent } from './components/product/product.component';
// Services
import { AuthService } from './services/auth.service';
export function restangularInit(RestangularProvider, AuthService) {
console.log(AuthService); // this is undefined
let token = AuthService.getToken(); //This is what I want to do
RestangularProvider.setBaseUrl('api');
RestangularProvider.setDefaultHeaders(
{'Authorization': 'Bearer ' + token},
);
}
@NgModule({
declarations: [
AppComponent,
ProductComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
MaterialModule,
FlexLayoutModule,
RestangularModule.forRoot(restangularInit)
],
providers: [
AuthService
],
entryComponents: [ProductComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Need to access service in function - angular - Stack Overflow
Create a static method getToken() then you can access it like you did in your code. ... export function restangularInit(RestangularProvider, ...
Read more >Export Function Models » Guy on Simulink - MathWorks Blogs
Let's use the following model as example. It contains a Bias and Unit Delay block executing at 0.001s and a Math Function block...
Read more >Angular Inject & Injection Functions - Patterns & Anti-Patterns
An Angular Injection Function is a synchronous function that directly or indirectly injects services using the inject() function.
Read more >PAN is missing EXPORT function - LIVEcommunity - 364963
Solved: I was trying to export the config for the first time and found that in Device/Setup/Operations I see no option for Export....
Read more >can't access service from another service, no documentation ...
This time, I'm using ember-cli, I generate a service. ... export function initialize(container, application) { application.inject('route', ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tairezzzz Yes, you are right. I shouldn’t pass service requires Restangular to Restangular as DI ) There is no problem now, thanks for your answer.
@timaxapa Yeah it was just a matter of injecting it…
RestangularModule.forRoot([MyService], restangularInit),