skip to Main Content

In my Angular app, I have a object structure to hold my constant variables. As such:

MY_VARS: {
    DEFAULT: 'BLOTTER',
    SELECT_A_ROLE: '-- Select a role --'
},

I also have a separate object in this file which holds values that I use for my dropdowns. As Such:

ROLE_TYPES: [
  {"value": "DEFAULT", "text":"-- Select a role--"},
  {"value":"FOO", "text":"Foo"},
  {"value":"BAR", "text":"Bar"}
]

I want to be able to reference values in either MY_VARS or ROLE_TYPES as constants so I don’t have to hard-code them. I have tried one suggestion to return as:

[{
  "value": function(){ return this.DEFAULT },
  "text": "-- Select a Role--"
}

And it compiles but the values are being returned empty.
Does anyone know a good way to reference these vars from within its own, or another object structure in the same file?

Thanks

2

Answers


  1. It seems like you just are not accessing the variables you have set up.

    If you have this:

    MY_VARS: {
        DEFAULT: 'BLOTTER',
        SELECT_A_ROLE: '-- Select a role --'
    },
    

    Then it seems you just need to point at those variable names, not strings.

    {
      "value": MY_VARS.DEFAULT,
      "text": MY_VARS.SELECT_A_ROLE
    }
    

    I can’t be totally sure how to access them though since you didn’t post the context of how these variables are stored. Are they class properties, constants, etc?

    For example, if these were all in a class:

    class myThing {
      MY_VARS = {
        DEFAULT: 'BLOTTER',
        SELECT_A_ROLE: '-- Select a role --'
      };
    
      ROLE_TYPES = [
        {"value": this.MY_VARS.DEFAULT, "text": this.MY_VARS.SELECT_A_ROLE},
        {"value":"FOO", "text":"Foo"},
        {"value":"BAR", "text":"Bar"}
      ]
    
      constructor() {
        console.log(this.ROLE_TYPES);
      }
    }
    
    //----
    const x = new myThing();

    You can run this code and see the output is as you expect it to be


    Editing based on comments below

    Ok, so if you have this file named my-constants.ts

    export const MY_VARS = {
      DEFAULT: 'BLOTTER',
      SELECT_A_ROLE: '-- Select a role --'
    };
    

    And then in another file you want to use those exported variables, you have to import them like this:

    import { MY_VARS } from '../path/to/my-constants';
    
    export class Example {
      ROLE_TYPES = [
        {"value": MY_VARS.DEFAULT, "text": MY_VARS.SELECT_A_ROLE},
        {"value":"FOO", "text":"Foo"},
        {"value":"BAR", "text":"Bar"}
      ]
    }
    
    Login or Signup to reply.
  2. This should be doable.

    Here is an example that is not Angular specific, but can easily be used inside an Angular component or service.

    const ROLE_TYPES = [
      {"key": "DEFAULT", "text":"-- Select a role--"},
      {"key": "FOO", "text":"Foo"},
      {"key": "BAR", "text":"Bar"}
    ]
    
    const MY_VARS = {
      DEFAULT: 'BLOTTER',
      FOO: 'Something',
      BAR: 'Else',
      SELECT_A_ROLE: '-- Select a role --'
    };
    
    const dereferenced = ROLE_TYPES.map(r => ({...r, value: MY_VARS[r.key]}));
    
    console.log(dereferenced);
    
    // or if you want to dereference a single item
    function getRoleByKey(key) {
      const role = ROLE_TYPES.filter(r => r.key = key)[0];
    
      return { ...role, value: MY_VARS[key]};
    }
    
    console.log(getRoleByKey('DEFAULT'));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search