[英]if binding in knockoutjs not working
I am trying to use if statement in knockoutjs with databinding: 我正在尝试在带有数据绑定的基因敲除js中使用if语句:
for example this if should be false and the text in div should be hidden: 例如,如果if为false且div中的文本应隐藏:
<!-- this is what i am trying to get working. -->
<div data-bind="if: little">rank : little</div>
My guess is this piece does not work as intended. 我的猜测是,这件作品无法按预期工作。 It should return false as on start the the clickCount is 0.
它应该返回false,因为clickCount在启动时为0。
this.little = function(){
return this.clickCount() > 5;
};
I have pasted the code of app.js and index.html. 我已经粘贴了app.js和index.html的代码。
this is app.js 这是app.js
var ViewModel = function (){
this.clickCount = ko.observable(0);
this.name = ko.observable('Tabby');
this.imgSrc = ko.observable('img/2.jpg');
this.imgAttribution = ko.observable('http://www.flickr.com/photos/big');
this.incrementCounter = function() {
this.clickCount(this.clickCount() + 1);
};
// this is not returning false as it should.
this.little = function(){
return this.clickCount() > 5;
};
}
ko.applyBindings(new ViewModel());
this is index.html 这是index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cat Clicker</title>
</head>
<body>
<div>
<h2 data-bind="text: name"></h2>
<div data-bind="text: clickCount"></div>
<!-- this is what i am trying to get working. -->
<div data-bind="if: little">rank : little</div>
<img src="" alt="cute cat" data-bind="click: incrementCounter, attr:{src: imgSrc}">
</div>
<script src="js/lib/knockout-3.2.0.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Hi you can use visible Binding of knockout 嗨,您可以使用可见的淘汰赛绑定
Following code will work for you: 以下代码将为您工作:
<div data-bind="visible: !little()">rank : little</div>
OR
<div data-bind="visible: little()">rank : little</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.