0

I am using angular2 with typescript. In one of the service I am setting and getting the value of a variable like this:

import {Injectable} from '@angular/core';

@Injectable()
export class UserProfileService {
    isLoggedIn: boolean;

    constructor() {

    }

    setLoggedInStatus(status) {
        console.log('status: ', status);
        this.isLoggedIn = status;
        console.log('this.isLoggedIn : ', this.isLoggedIn)    //setting to true or false here
    }

    getLoggedInStatus() {
        return this.isLoggedIn;
    }
}

but when I am trying to get the value using getLoggedInStatus(), I am getting undefined each time. Why is this happening ?

setting here

import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {SharedService} from '../../shared.service';
import {UserProfileService} from '../../authComponent/login/user-profile.service';

@Component({
    selector: 'login',
    templateUrl: 'app/components/authComponent/login/login.component.html',
    providers: [SharedService, UserProfileService]
})

export class LoginComponent implements OnInit {
    matched: boolean = false;

    constructor(private sharedService: SharedService, private userService: UserProfileService) {
    }

    ngOnInit() {
        this.myForm = new FormGroup({
            email: new FormControl('', [Validators.required]),
            password: new FormControl('', [Validators.required])
        })
    }

    submit({value, valid}) {
        if (!valid) {
            return;
        }
        else {
            this.sharedService.getData('users')
                .subscribe(result => {
                    console.log('result: ', result, 'value passed: ', value)
                    result.forEach((record) => {
                        if ((record.email == value.email && record.password == value.password) && !this.matched) {
                            this.matched = true;
                        }
                        else {
                            this.matched = false;
                        }
                    });

                    if (!this.matched)
                        console.log('wrong credentials entered');
                    this.userService.setLoggedInStatus(this.matched);
                })

        }
    }
}

getting here

import {Injectable} from '@angular/core';
import {
    CanActivate,
    CanActivateChild,
    Route,
    Router,
    ActivatedRouteSnapshot,
    RouterStateSnapshot
} from '@angular/router';

import {UserProfileService} from './authComponent/login/user-profile.service';

@Injectable()
export class CanActivateAuthGuard implements CanActivate, CanActivateChild {
    constructor(private userProfileService: UserProfileService, private router: Router) {
    }

    canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return this.canActivate(next, state);
    }

    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        console.log('this.userProfileService.getLoggedInStatus: ', this.userProfileService.getLoggedInStatus())
        if (this.userProfileService.getLoggedInStatus()) {
            return true;
        }
        this.router.navigate(['/login'], {queryParams: {redirectTo: state.url}});
        return false;
    }
}
8
  • 1
    You are using setLoggedInStatus in one component and getLoggedInStatus in another component right? Commented Mar 10, 2017 at 18:50
  • how you are setting and getting? Commented Mar 10, 2017 at 18:50
  • You're probably calling it on another instance of the service. Impossible to say why if you don't post an example reproducing the problem. Commented Mar 10, 2017 at 18:50
  • @camaron: Yes in different components Commented Mar 10, 2017 at 18:51
  • @BhushanGoel okay, your problem is what JB Nizet pointed. You must be sure that UserProfileService is provided by only one NgModule and no other place. Commented Mar 10, 2017 at 18:53

3 Answers 3

1

Services in angular are injectables meaning, they are invoked whenever needed. If you are using configuration variables and global variables you can use them by declaring in the AppModule as below

    import {Component, NgModule} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'

    @Component({
      selector: 'my-app',
      template: `
        <div>
          <h2>{{name}} Sample</h2>
          {{myConfiguration | json}}
        </div>
      `,
    })
    export class App {
      name:string;
      myConfiguration:any=myConfiguration;
      constructor() {
        console.log(myConfiguration);
        this.name = 'Global Configuration'
      }
    }

    @NgModule({ 
      imports: [ BrowserModule ],
      declarations: [ App ],
      bootstrap: [ App]
    })
    export class AppModule {
        var myConfiguration={id:1,devmode:true};  

    }

LIVE DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

Did you call getLoggedInStatus() before ever calling `setLoggedInStatus(..)?

If so, you never initialized your isLoggedIn boolean member variable, and as such, it remains as undefined.

You can bypass this problem by initializing isLoggedIn when you declare it

export class UserProfileService {
   isLoggedIn = false;  // or true
.....

And notice i didn't have to declare the type - Typescript inferred it from the value it was initialized with.

EDIT:

With your updated post, i noticed that you added your service to your component's list of providers. Based on how you want to use that service, you don't want to do that. Adding a service to a component's list of providers is more or less saying to Angular that i don't want this component to share an instance of my Service with anyone else - i want this component to have its own instance of that Service.

Based on your usage, you actually want to add that service to your AppModule's list of providers instead.

see this other question for almost the same scenario

Comments

1

To have a singleton service instance just add that service as a provider in one module and don't provide that service in other places.

The easiest way is to add the service to app.modules.ts providers.

app.module.ts:

@NgModule({ 
  ...
  providers:[
     UserProfileService
  ]
})
export class AppModule {}

That will do the job.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.