0

I am new to Ionic and was wondering if there is a way to dynamically set a global variable and then have it for the rest of the app.

I have an app where on home page I ask user for Phone number and then verify their identity. Once verified I need to store/remember/retrieve for the rest of the app.

Is there a way I can do that rather than passing around it as a parameter?

2 Answers 2

1

You can use localStorage to save the phone number :

 $window.localStorage.setItem("phoneNumber",phoneNumber);
 $window.localStorage.getItem("phoneNumber");
Sign up to request clarification or add additional context in comments.

Comments

1

You should be managing your state in a service (ionic calls them providers, generate one with 'ionic generate provider').

The official tutorial 'Tour of Heroes' covers this. You can find the appropriate section here https://angular.io/docs/ts/latest/tutorial/toh-pt4.html.

basically you create a service:

@Injectable()
export class MyService{
  public phoneNumber;
}

and use it from all your components like this:

export class MyComponent{
  constructor(private myService: MyService){}

  setPhone(number: number){
    if(validateNumber(number)){
      this.myService.phoneNumber = number;
    }
  }
}

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.