代码之家  ›  专栏  ›  技术社区  ›  Simsons

双向数据绑定不适用于敲除

  •  0
  • Simsons  · 技术社区  · 6 年前

    1. 单击“添加更多”将新值推送到可观察数组将新文本框添加到UI
    2. 单击Save可在div的文本框中显示值。

    var model = function() {
      var self = this;
      self.inventoryItems = ko.observableArray();
    
      myArray = ["Value1", "V1lue2", "Value3", "Value4"];
      self.inventoryItems(myArray);
    
      self.addItems = function(vm) {
        self.inventoryItems.push('New Item');
      }
    
      self.SaveInventory = function(data) {
        $('#htmlBlock').html(myArray);
        console.log(myArray)
      };
    
    };
    
    ko.applyBindings(new model());
    ul {
      list-style: none;
    }
    
    li {
      margin-top: 5px;
    }
    
    .info-text,
    a,
    button {
      margin-top: 20px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
    
    <ul data-bind="foreach: inventoryItems">
      <li>
        <input data-bind="value: $data" />
      </li>
    </ul>
    <div>
      <a data-bind="click: addItems">+ Add more</a>
    </div>
    <button type="submit" class="btn btn-accent" data-bind="click: SaveInventory">Save changes</button>
    <div class='info-text' id="htmlBlock">
    </div>

    1 回复  |  直到 6 年前
        1
  •  2
  •   xec    6 年前

    在可观察数组中不需要单独引用底层数组,它只会混淆事情。使用读取值 self.inventoryItems() .

    实时查看模型外观的一个好方法是使用带有 data-bind="text: ko.toJSON($data, null, '\t')"

    function EditableField(initialValue) {
      // each value you want to be able to have a two-way binding for needs to be observable
      this.value = ko.observable(initialValue);
    }
    
    var model = function() {
      var self = this;
      self.inventoryItems = ko.observableArray(["Value1", "V1lue2", "Value3", "Value4"].map(function(item) {
        // run each array value through constructor
        return new EditableField(item);
      }));
    
      self.addItems = function(vm) {
        self.inventoryItems.push(new EditableField('New Item'));
      }
    
      self.SaveInventory = function(data) {
        console.log(ko.toJS(self.inventoryItems)); // fetch the updated array
      };
    
    };
    
    ko.applyBindings(new model());
    ul {
      list-style: none;
    }
    li {
      margin-top: 5px;
    }
    .info-text,
    a,
    button {
      margin-top: 20px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
    
    <ul data-bind="foreach: inventoryItems">
      <li>
        <!-- use textInput binding for live updates, bound to the value property from constructor -->
        <input data-bind="textInput: value" />
      </li>
    </ul>
    <div>
      <a data-bind="click: addItems">+ Add more</a>
    </div>
    <button type="submit" class="btn btn-accent" data-bind="click: SaveInventory">Save changes</button>
    
    <!-- display the model in real time -->
    <pre class='info-text' data-bind="text: ko.toJSON($data, null, '\t')">
    </pre>