我做了我的第一个knockout.js应用程序http://jsfiddle.net/Pqs7e/
对于显示应用程序部分(图书部分、关于部分),我使用jquery $("#id").show()。我觉得这条路不对。我如何通过视图模型的机制来做到这一点呢?
发布于 2012-02-12 10:11:24
我会用特殊的state observable来做这件事,它会确定要显示哪个div:
function ViewModel(){
var self = this;
self.state = ko.observable();
...
}然后你只需像这样绑定它:
<div id="books" data-bind="visible: state() === 'books'>...</div>
<div id="about" data-bind="visible: state() === 'about'>...</div>并像这样在状态之间切换:
this.get('#books', function() {
self.state("books");
});
this.get('#about', function() {
self.state("about");
});发布于 2012-08-15 08:53:55
另一种方法是使用模板:
<div data-bind="template: state">
Template renders here
</div>然后,您的部分可以像这样定义在某个地方(在同一文件中或其他地方):
<script id="books" type="text/html">
Your markup here...
</script>
<script id="about" type="text/html">
Your markup here...
</script>https://stackoverflow.com/questions/9242817
复制相似问题