如何将数组中的objcet intizialize并推送到构造函数中?

时间:2017-11-07 11:07:22

标签: angular typescript

申请中有#34;目标"下拉,下拉中有两个元素。 我用两个参数和construcor创建了Model(NameValueModel):

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

在我要创建的主要课程中:

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"

到目前为止,我所做的是:

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"));
 }

最后我需要提供:let target = this.selectedTargets。 value ;

但是,谴责者不会让我把价值,只有"价值观" this.selectedTargets的;

我如何解决这个问题?或者这种实施方式还可以吗?

1 个答案:

答案 0 :(得分:1)

我无法理解你,但我想这就是你所需要的 你首先需要启动数组

 selectedTargets:NameValueModel[]=[]; 
    constructor() {
      this.selectedTargets.push({name:"Current window", value:"_self"}); 
      this.selectedTargets.push({name:"New window", value:"_blank"});
    }