2017-08-09 68 views
-1

我有一個Typescript文件並返回一個數組以用於綁定* ngFor。如何重新設置數組值

OLD:

getYear(): any { 
    return [ 
     { name: "2014", value: 2014 }, 
     { name: "2015", value: 2015 }, 
     { name: "2016", value: 2016 }, 
     { name: "2017", value: 2017 }, 
     { name: "2018", value: 2018 }, 
     { name: "2019", value: 2019 }, 
     { name: "2020", value: 2020 }, 
    ]; 
} 

這是硬編碼值

現在,如何讓我喜歡舊的我用這之前數組類型的?

NEW:

return [2014,2015,2016,....]; 

這個數組是從服務器端。

回答

0

我創造了這個作爲容器:

years = Array<{ name: string , value: number }> = []; 

然後我用新值我在服務中獲得填充容器。

for (let i = 0; i < this.yearValues.length; i++) { 
      this.years.push({ name: this.yearValues[i], value: +this.yearValues[i] }); 
     } 
     this.optionsYear = this.years;