当可见性绑定为true时如何调用函数

How to call a function when a visibility binding is true?

本文关键字:调用 函数 何调用 绑定 可见性 true      更新时间:2023-09-26

是一个如何在绑定可见时执行javascript函数的示例:true意味着BooleanIndicator()返回true,假设调用的函数有e作为参数,其中e是一个事件。

<div data-bind="visible: shouldShowMessage">
    You will see this message only when "shouldShowMessage" holds a true value.
</div>
<div >
 Also show this div when the above div is visble
<div>

绑定依赖于视图模型中的数据。如果BooleanIndicator()是视图模型的一个可观察属性,你可以创建一个计算函数,当布尔指示器()改变时,它应该被调用

self.ComputedFunction = ko.computed(function() {
    if (self.BooleanIndicator()){
        //Do something - I'm visible
    } else {
        // Do something else
    }
});