【问题标题】:knockout binding is not working without parenthesis没有括号,淘汰赛绑定不起作用
【发布时间】:2016-06-07 01:55:24
【问题描述】:

我对淘汰赛 js 有点陌生。我有一个基于可观察变量启用或禁用的按钮。 看来绑定仅在我将其与括号一起使用时才有效。

有人知道为什么会这样吗?我的理解是我们应该能够在没有括号的情况下绑定可观察变量

self.noTaxResidencyChecked = ko.observable(false);

//works fine
<button data-bind="enable: !noTaxResidencyChecked()"></button>

//doesn't work
<button data-bind="enable: !noTaxResidencyChecked"></button>

【问题讨论】:

    标签: knockout.js


    【解决方案1】:

    这归结为对象的真实性。这是在 javascrpt 中工作的默认方式。当你使用'!'运营商标准 javascript 比较开始

    var x = ko.observable(null);

    !!x // true - observable 本身是一个评估为 true 的对象 !!x() // false - 您现在正在查看 observable 中的对象,该对象为 null,计算结果为 false

    无论何时使用 !标记中的运算符

    // true if it evaluates to true
    <button data-bind="enable: !noTaxResidencyChecked()"></button>
    
    // true no matter what because the property is an observable object
    <button data-bind="enable: !noTaxResidencyChecked"></button>
    

    你可以做什么

    <button data-bind="disable: noTaxResidencyChecked"></button>
    // since you are not using a '!' knockout does its standard look into the object
    

    但这可能不符合您的需求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-10
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 2013-03-01
      • 2013-12-27
      • 2012-11-08
      相关资源
      最近更新 更多