I am trying to use if statement in knockoutjs with databinding:
for example this if should be false and the text in div should be hidden:
<!-- 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.
this.little = function(){
return this.clickCount() > 5;
};
I have pasted the code of app.js and index.html.
this is 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
<!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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.