0

http://jsfiddle.net/user/jsjs/fiddles/

我在<select>使用 Knockout.js 选项绑定到嵌套视图模型时遇到问题。我错过了什么吗?我尝试了 userFSR().facilityOptions、mainView().userFSR().facilityoptions 和 userFSR.facility()。所有这些都引发了 javascript 运行时错误。唯一没有引发运行时错误的是下面的 html 片段,但下拉列表在呈现后为空。谢谢您的帮助。

        <div id="userFSR">
            <select id="selectFacility" 
                data-bind="options: userFSR.facilityOptions,
                    selectedOptions: userFSR.facilitySelected,
                    optionsCaption: 'Choose facility...',
                    optionsValue: function(item){ 
                        return item.facility_ID
                    },
                    optionsText: function(item){ 
                        return item.facility_name
                    }">
            </select>
        </div>

下面是这样做 ko.toJSON(self, null, 2) 后的viewModel内容

self.toggleVM = function () {
    if ($("pre#viewModel").css("display").search("none") != -1) {
        $("pre#viewModel").html(ko.toJSON(self, null, 2));
        $("pre#viewModel").show();
    }
    else
        $("pre#viewModel").hide();
}

视图模型输出:

   {
      "btnCancelValue": "Cancel",
      "userBeforeEdit": {
            /* omitted */
      },
      "users": [
        {
            /* omitted */
        },
        {
            /* omitted */
        }
      ],
      "user": {
        /* omitted */
      },
      "operationStatus": "omitted",
      "currentIni": "Q",
      "initials": [
        {
          "initial": "+",
          "apiUrl": "/* omitted */"
        },
        {
          /* omitted */
        }
      ],
      "userFSR": {
        "facilityOptions": [
          {
            "$id": "1",
            "s_FacilityService": [],
            "facility_ID": 531,
            "facility_name": "City 1, State 1",
            "Facility_Code": null
          },
          {
            "$id": "2",
            "s_FacilityService": [],
            "facility_ID": 648,
            "facility_name": "City 2, State 2",
            "Facility_Code": null
          },
          {
            "$id": "3",
            "s_FacilityService": [],
            "facility_ID": 663,
            "facility_name": "City 3, State 3",
            "Facility_Code": null
          }
        ],
        "serviceOptions": [],
        "roleOptions": [],
        "roleSelected": [],
        "FsrCollection": []
      },
      "userPrefFSR": {
        "fsrPrefCollection": []
      }
    }
4

0 回答 0