2

即使我可以在浏览器中看到,来自 eventschemas 的数据也没有绑定。这可以在我在底部提供的 $data 值中看到。请注意,这是对问题和架构的最小再现改不了。。

我的 html

<div data-bind="with: g">

    <div><input type="text" class="form-control" data-bind="value: gname" /></div>

         <div>
              <table>

                  <tbody>
                     <tr data-bind="with:gdetails">
                        <td>
                           <select data-bind="options: eventschemas, optionsText: 'schema', value:eventschemacondition.schema"></select>

                         </td>

                    </tr>
                   </tbody>
               </table>
           <pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
        </div>                
 </div> 

我的JavaScript:

 <script type="text/javascript">
        var eventschemas = [{ "schema": "Test"}, { "schema": "Another Test" }];

        var AppScope = function () {
             function EventSchemaCondition(data) {
                this.schema = ko.observable(data.schema);
                };
            function Gdetails(data) {
                this.eventschemacondition = ko.observable(data.eventschemacondition);
            };
            function G(data) {
                this.gname = ko.observable(data.gname);
                this.gdetails = ko.observable(data.gdetails);
            };
                function GsViewModel() {
                    var self = this;
                   self.g = ko.observable(new G({ gname: "", gdetails: new Gdetails({ eventschemacondition:new EventSchemaCondition({ schema: "" }) }) }));

                }
                ko.applyBindings(new GsViewModel());
          }();
    </script>

这就是我的 $data 的样子:

{
  "gname": "tttt",
  "gdetails": {
    "eventschemacondition": {
      "schema": ""
    }
  }
}

如您所见,即使我可以在浏览器的下拉列表框中看到值,“eventschemacondition”中的“模式”也是空的。

衷心感谢任何帮助

谢谢

4

1 回答 1

2

您需要eventschemacondition().schemavalue绑定中调用select

var eventschemas = [{ "schema": "Test"}, { "schema": "Another Test" }];

var AppScope = function () {
    function EventSchemaCondition(data) {
        this.schema = ko.observable(data.schema);
    }
    
    function Gdetails(data) {
        this.eventschemacondition = ko.observable(data.eventschemacondition);
    }
    
    function G(data) {
        this.gname = ko.observable(data.gname);
        this.gdetails = ko.observable(data.gdetails);
    }
    
    function GsViewModel() {
        var self = this;
        self.g = ko.observable(
        new G({ 
           gname: "", 
           gdetails: new Gdetails({ eventschemacondition:new EventSchemaCondition({ schema: "" })})
       }));
    }
    
    ko.applyBindings(new GsViewModel());
  }();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div data-bind="with: g">
    <div><input type="text" class="form-control" data-bind="value: gname" /></div>
        <div>
            <table>
                <tbody>
                    <tr data-bind="with:gdetails">
                        <td>
                            <select data-bind="options: eventschemas, optionsText: 'schema', value:eventschemacondition().schema"></select>
                        </td>
                    </tr>
                   </tbody>
               </table>
            <pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
        </div>                
 </div>

于 2014-12-18T14:20:47.433 回答