KnockoutJS - Ifnot 绑定
Ifnot 绑定是 if 绑定的否定。它只是 if 绑定的另一种形式。
语法
ifnot: <binding-condition>
参数
参数是您要检查的条件。如果条件评估为 true 或类似 true 的值,则将处理给定的 HTML 标记。否则,它将从 DOM 中删除。
如果参数中的条件包含可观察值,则每当可观察值发生变化时都会重新评估条件。相应地,将根据条件结果添加或删除相关标记。
示例
让我们看看以下示例,该示例演示了 ifnot 绑定的使用。
<!DOCTYPE html> <head> <title>KnockoutJS ifnot binding</title> <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type = "text/javascript"></script> </head> <body> <p><strong>Product details</strong></p> <table border = "1"> <thead> <th>Product Name</th><th>Price</th><th>Nature</th> </thead> <tbody data-bind = "foreach: productArray "> <tr> <td><span data-bind = "text: productName"></span></td> <td><span data-bind = "text: price"></span></td> <td data-bind = "ifnot: $data.price < 200 ">Expensive</td> </tr> </tbody> </table> <script type = "text/javascript"> function AppViewModel() { self = this; self.productArray = ko.observableArray([ {productName: 'Milk', price: 100}, {productName: 'Oil', price: 10}, {productName: 'Shampoo', price: 1200} ]); }; var vm = new AppViewModel(); ko.applyBindings(vm); </script> </body> </html>
输出
让我们执行以下步骤来查看上述代码的工作原理 −
将上述代码保存在 if-not-bind.htm 文件中。
在浏览器中打开此 HTML 文件。
此示例将填充第三列,该列根据价格讨论产品的性质(昂贵与否)。请注意,使用 $data 绑定上下文访问单个属性。
knockoutjs_declarative_bindings.html