0

I have a nested Object which will contain information like userId, token, name, email, phone, company name etc till the user logouts. I need to use certain values of the object when calling an API.

Is there a way to globally set and get the values of the object in each of my component. Currently what I have done is I have setter and getter and I set and get the values individually but it's very time consuming and since I get the values seperately I have to form the Object again in each of the component. Current approach :

  1. Create a service class and create two functions.

    setEmailData(info) {
    this.info = info;
    }
    
    getEmailData() {
    const temp = this.info;
    //  this.clearData();
    return temp;
    }
    

I do this for atleast 10 values. Is there a way I can create and set and fetch the values of an object directly? My actual object looks something like this

userData = {
    border: {
      border: '',
      visible: true
    },
    header: {
      title: '',
      visible: true
    },
    isLoggedIn: false,
    uid: '',
    uid_loader: false,
    user: {
      UserID: '',
      company: 'dev',
      emaild: '',
      lastname: '',
      name: '',
      phone: '',
      token: '',
      url: ''
    }
  };
0

2 Answers 2

2

Just create a global service.

ng g service userService

In your service add a private variable

private userdata: IUserData | undefined

add a public get and set

public get Userdata(): IUserData | undefined {
    return this.userdata
}

public set Userdata(value: IUserData) {
    this.userdata = value
}

Then add the service into your components in the constructor.

And declare the interface

export interface IUserdata {
  border: {
    border: string,
    visible: boolean
  },
  header: {
    title: string,
    visible: boolean
  },
  isLoggedIn: boolean,
  uid: string,
  uid_loader: boolean,
  user: {
    UserID: string,
    company: string,
    emaild: string,
    lastname: string,
    name: string,
    phone: string,
    token: string,
    url: string
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What is IUserData in private userdata: IUserData | undefined?
It is an interface in which you declare object structure you have for the user.
This is just the name of the interface which defines your datatype, using typescript gives you the ability to declare types. I added the definition to my answer so it correlates to your object.
oh..okay thanks @JamieVangeysel. I suppose I can just loop through the object and set and get the values right ?
0

One option is Reactive Form. Create the form schema from your User Object in the Oninit hookup.

Once you create the object, then you can easily set and read the values of the form object.

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.