<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/knockout-3.4.0.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div data-bind="visible:shouldShowMessage"> You will see this message only when "shouldShowMessage" holds a ture </div> <script type="text/javascript"> var myViewModel = { shouldShowMessage:ko.observable(true) }; ko.applyBindings(myViewModel); setTimeout(function(){ myViewModel.shouldShowMessage(false); },2000); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/knockout-3.4.0.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div data-bind="visible:myValues().length>0"> You will see this message only when "myValues" holds a ture </div> <script type="text/javascript"> var myViewModel = { myValues:ko.observableArray([]) }; ko.applyBindings(myViewModel); setTimeout(function(){ myViewModel.myValues.push("name"); },2000); </script> </body> </html>
来源:
http://knockoutjs.com/documentation/visible-binding.html