繁体   English   中英

KnockoutJS Observable 未在模板中更新

[英]KnockoutJS Observable not updating in template

我在 Knockout JS 中的类更新的跨度上进行了切换。 当我注销 Observable 的值时,它是正确的。 但是,复选框的类不会按预期切换。 这是我正在使用的 JS 和模板代码...

JS

viewModel.toggleRequiredAnswer = function(self, index) {
    // Other unrelated code using index param here
  var checkedTest = viewModel
      .data()
      .conditionalRequired.requiredIf.some(function(arr) {
        return arr === index;
    });
    self.isChecked = ko.observable(checkedTest);
};

KnockoutJS 模板

<ul class="list" data-bind="foreach: list">
    <li>
        <span
          class="glyphicon glyphicon-admin"
          data-bind="
                    click: $parent.toggleRequiredAnswer($data, $data.value),
                    css: $data.isChecked() ? 'glyphicon-check' : 'glyphicon-unchecked'"
        >
            </span>
        <span data-bind="text: $data.text"></span>
    </li>
</ul>

当函数toggleRequiredAnswer被调用时,你用一个新的 observable 覆盖self.isChecked属性,并且淘汰视图正在失去与它的关系。 函数toggleRequiredAnswer应该是这样的:

viewModel.toggleRequiredAnswer = function(self, index) {
    // Other unrelated code using index param here
  var checkedTest = viewModel
      .data()
      .conditionalRequired.requiredIf.some(function(arr) {
        return arr === index;
    });
    self.isChecked(checkedTest);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM