Knockout JS は初めてです。ネストされた配列を次のようにバインドする必要があります
名前:ドロップダウン
メール:選択したユーザーの名前
連絡方法の種類: ContactInfo からの連絡方法の種類が選択されたドロップダウン
連絡先の値: ContactInfo からの実際の値
名前、電子メール、連絡先の値が機能しています。連絡方法の種類の値をドロップダウンで選択する方法と、連絡先の値にバインドする必要があることを知る必要があります。
次のエラー エラーが表示されます: プロパティ 'ContactMethodType' の値を取得できません: オブジェクトが null または未定義です
function LifelineViewModel() {
this.lifelines = ko.observableArray([{
Name: "",
Email: "",
ContactInfo:
{
ContactMethodType: "",
ContactValue: ""
}
}]);
this.selectedLifeline = ko.observable();
this.contactTypes = ko.observableArray([{Name: ''}]);
this.selectedContactInfo = ko.dependentObservable(function () {
if (this.selectedLifeline() === undefined) return null;
return this.selectedLifeline().ContactInfo;
}, this);
this.selectedContactMethodType = ko.dependentObservable(function () {
if (this.selectedContactInfo() === undefined) return null;
return this.selectedContactInfo().ContactMethodType;
}, this);
}
HTMLコード
<select data-bind="options: lifelines, optionsText: 'Name', value: selectedLifeline"></select>
<p><span data-bind="text: selectedLifeline().Email"></span></p>
<p><span data-bind="text: selectedContactInfo().ContactMethodType + ' ' + selectedContactInfo().ContactValue"></p>
<select data-bind="options: contactTypes, optionsText: 'Name', value: selectedContactMethodType"></select>