简体   繁体   English

我如何在div中将数据绑定=“ visible:active”

[英]How do I data-bind = “visible : active” in div

hi this is my code snippet: 嗨,这是我的代码片段:

<div class="showtimes" data-bind="visible: showHide">
 <div data-bind="template: { name: 'movie-grouped-showtimes-template', data: $data }"></div>
</div> 

I want to toggle showHide off and on using the following: 我想使用以下showHide关闭和打开showHide

<a class="icon arrow" data-bind="click: $parent.showtimes">

can't I just set up a variable showHide in my view Model, like the following: 我不能只在视图模型中设置变量showHide ,如下所示:

self.showHide = ko.observable(false) ... Hide self.showHide = ko.observable(false) ...隐藏

showHide(true); ... show ... 节目

and can I set it using the click : $parent.showtimes , like in the following: 我可以使用click : $parent.showtimes设置它click : $parent.showtimes ,如下所示:

<a class="icon arrow" data-bind="click: $parent.showtimes"></a>        

You just have to set a function in the viewmodel that binds to the button's click (or modify an existing one, like showTimes in this case) to toggle the value of showHide. 您只需要在viewmodel中设置绑定到按钮单击的函数(或修改现有的函数,在这种情况下,如showTimes)即可切换showHide的值。

Here's a simple example: http://jsfiddle.net/EfrainReyes/LNzDL/ 这是一个简单的示例: http : //jsfiddle.net/EfrainReyes/LNzDL/

var vm = {
    showHide: ko.observable(false),
    toggle: function() {
        this.showHide( !this.showHide() );
    }
};

ko.applyBindings(vm);

I didn't add any other elements to the example because the question seems to be targeted more towards showing/hiding the div. 我没有在示例中添加任何其他元素,因为该问题似乎更倾向于显示/隐藏div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM