我已经使用淘汰赛绑定工作了几天而且我已经定义了一个选择如下:
<select data-bind="options: availableLanguages, value: selectedLanguage, optionsText: 'Name', optionsValue: 'Id'"></select>
我从以下位置收到语言列表(availableLanguages
)和默认语言(selectedLanguage
):
internal class AvailableLanguages
{
public string Name { get; set; }
public string Id { get; set; }
}
AvailableLanguages userLanguage = new AvailableLanguages();
List<AvailableLanguages> languagesList = new List<AvailableLanguages>();
languagesList.Add(new AvailableLanguages()
{
Name = "English",
Id = "en"
});
languagesList.Add(new AvailableLanguages()
{
Name = "Francais",
Id = "fr"
});
languagesList.Add(new AvailableLanguages()
{
Name = "Espanol",
Id = "es"
});
languagesList.Add(new AvailableLanguages()
{
Name = "Italian",
Id = "it"
});
languagesList.Add(new AvailableLanguages()
{
Name = "Espanol",
Id = "es"
});
userLanguage.Id = "en";
userLanguage.Name = "English";
我填补了淘汰赛的观察结果:
_mVM.availableLanguages = ko.observableArray();
_mVM.selectedLanguage = ko.observable();
_mVM.availableLanguages(languagesList);
_mVM.selectedUnit(userLanguage);
我设法只显示来自availableLanguages
的姓名,但我需要默认语言为Espanol,但始终是列表中的第一种语言,在我的情况下是英语。
可以将默认语言更改为Espanol吗?
答案 0 :(得分:1)
只需在selectedLanguage
_mVM.availableLanguages(languagesList);
的值即可
selectedLanguage("es");