question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access service in export function restangularInit

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
timaxapacommented, Mar 24, 2017

@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.

1reaction
mackelitocommented, Mar 20, 2017

@timaxapa Yeah it was just a matter of injecting it…

RestangularModule.forRoot([MyService], restangularInit),

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found