2

I've written a array for store language options.

public languageOptions:Array<Object> = [
    {
    name: 'English',
    value: 'en'
    },
    {
    name: 'France',
    value: "fr"
    }
];

And I want to set English as default language. I did it in following way,

private static readonly DEFAULT_LANGUAGE: string = 'en';

But I want to set 'en' value directly from the object array like this,

private static readonly DEFAULT_LANGUAGE: string = languageOptions[0].value;

But when I write that, it gives following error

[ts] Cannot find name 'languageOptions'

Can someone please help me to solve this problem. Please advice how to access 'en' from array object. Thank you.

1
  • You cannot access an instance property like languageOptions from a static method--it doesn't make any sense. Perhaps you want languageOptions to be status too. Commented Sep 3, 2017 at 6:30

2 Answers 2

3

You should use this

export class App {
 languageOptions = [
    {
    name: 'English',
    value: 'en'
    },
    {
    name: 'France',
    value: "fr"
    }
];
 private readonly DEFAULT_LANGUAGE = this.languageOptions[0].value;
}

DEMO

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

2 Comments

then it returns, "[ts] Property 'languageOptions' does not exist on type 'typeof AppComponent'." error
did you check the demo
0

You can't access an instance member (aka non static, languageOptions in your case) from a static member initialization (for DEFAULT_LANGUAGE).

Also do not type you array as Array<object>, this will not let the compiler do inference and help you out after. Just use :

public static languageOptions = [
    {
    name: 'English',
    value: 'en'
    },
    {
    name: 'France',
    value: "fr"
    }
];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.