2017-09-26 202 views

回答

5

你只需要值的陣列重視selectedCities變量,以結合這給模型。

在你的情況下,屬性是一個object其中包含許多屬性。

value:{id:1, name: 'New York', cityCode: 'NY'} 

的解決方案是map陣列項目,以獲得您想要的值。

例如,這會從你的下拉元素預選拳頭 items

this.selectedCities=this.cities.slice(0,2).map(a=>a.value)); 

如果你想從given陣列預選值,你應該使用filter方法。

let arrayOfValues=['NY','IST']; 
this.selectedCities=this.cities.filter(a => arrayOfValues.includes(a.value.cityCode)).map(a => a.value)); 

完整示例here

0

選定的城市存儲在selectedCities數組中。由於它是雙向綁定,只需填充該arry,它就會反映在視圖中。

import {SelectItem} from 'primeng/primeng'; 

let cities: SelectItem[] = [ 
    { label : "Rome"  , value : "ro" }, 
    { label : "London" , value : "lo" }, 
    { label : "Paris" , value : "pa" }, 
    { label : "New York" , value : "ny" } 
] 

let selectedCities: string[] = ["lo", "ny"] // This will pre-select the cities in your dropdown 
0

有一個好方法 您可以爲每個選項定義值。 然後將變量selectedCities定義爲您想要的值爲defult。 它會使初始化時選擇該VAL選項的角度。

let Cities: SelectItem[] = [ 
    { label : "Rome"  , value : "ro" }, 
    { label : "London" , value : "lo" }, 
    { label : "Paris" , value : "pa" }, 
    { label : "New York" , value : "ny" } 
] 

selectedCity = "ro"; 

這將設置選定的calue來侮辱羅馬。

(*感謝Jeremy Thille,我抄錄了我的一部分代碼。)