0

There is in application "target" drop-down, witn two elements in that drop-down. I created Model (NameValueModel) with two parametars and construcor:

1. name:string
2. value:string
3. constructor(name, value) { this.name=name; this.value=value; }

In main class I want to create:

1. list of this objects
2. initialize the list in constructor
3. After that push two new NameValueModel-s in that list. (This step is problem for me)

I need to push this objects: 
a) name: "New window", value: "_blank"
b) name: "Current window", value: "_self"

What I do so far is:

1. Create Model with 2 parmas and constrcutor

2. In main class create 
a) List of objects: selectedTargets: NameValueModel[]; 
b) Trying to push objects into lsit:
 constructor() {
   this.selectedTargets.push(new NameValueModel("Current window", "_self")); 
   this.selectedTargets.push(new NameValueModel("New window", "_blank"));
 }

On the end I need to provide: let target = this.selectedTargets.value;

But compailer does not give me to put Value, only "Values" this.selectedTargets.values;

How I can fix this? Or this way of implemetation is ok?

1
  • this.selectedTargets[0].name or this.selectedTargets[0].value Commented Nov 7, 2017 at 11:23

1 Answer 1

2

I can't understand you very well, but I guess this is what you need, you need to initiate the array first

 selectedTargets:NameValueModel[]=[]; 
    constructor() {
      this.selectedTargets.push({name:"Current window", value:"_self"}); 
      this.selectedTargets.push({name:"New window", value:"_blank"});
    }
Sign up to request clarification or add additional context in comments.

2 Comments

and you could declare your NameValueModel as Interface like that export interface NameValueModel{ name:string; value:string; }
Yes thank you. Both answers are exactly what I need.

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.