Javascript 将可见参数绑定到 Knockout 中的“or”语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8803484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:35:16  来源:igfitidea点击:

Binding a visible parameter to an 'or' statement in Knockout

javascriptknockout.js

提问by PlTaylor

I would like to bind a visible property to be true when one of two conditions are true. Something like the following

当两个条件之一为真时,我想将可见属性绑定为真。类似于以下内容

 <tr data-bind="visible: active || $parent.displayDeactive">....</tr>

My code works when I do one or the other bindings but not when I put the || in there. I haven't found any documentation that says I can put any logic in this binding, but if I can't do it directly what is the est way to do it since I am binding a property of a template and one object of the $parent viewmodel.

当我执行一个或其他绑定时,我的代码有效,但当我放置 || 时无效 在那里。我还没有找到任何文档说我可以在这个绑定中放置任何逻辑,但是如果我不能直接做到这一点,那么最好的方法是什么,因为我绑定了模板的属性和 $ 的一个对象父视图模型。

回答by RP Niemeyer

If you are using the value of an observable in an expression then you need to reference them as a function. So, if activeand displayDeactiveare observables you would do:

如果您在表达式中使用 observable 的值,则需要将它们作为函数引用。所以,如果activedisplayDeactive是可观察的,你会这样做:

data-bind="visible: active() || $parent.displayDeactive()"

data-bind="visible: active() || $parent.displayDeactive()"

There are a few ways to move it to the view model, you could:

有几种方法可以将其移动到视图模型,您可以:

  • create a computed observable on the child (function would need to be able to reference the parent)
  • create a function on the parent that takes in the child and returns your value (bindings are executed in a computed observable, so it will fire again when any observable that it accesses changes)
  • create a function on the child that takes in the parent and returns the value (same note as above)
  • 在子对象上创建一个计算的可观察对象(函数需要能够引用父对象)
  • 在父级上创建一个函数,该函数接收子级并返回您的值(绑定在计算出的 observable 中执行,因此当它访问的任何 observable 发生更改时,它将再次触发)
  • 在子级上创建一个函数,该函数接受父级并返回值(与上面相同的注释)

Sample of logic in the binding and using a function on the parent here: http://jsfiddle.net/rniemeyer/f6ZgH/

绑定中的逻辑示例并在此处使用父级上的函数:http: //jsfiddle.net/rniemeyer/f6ZgH/

回答by John Papa

Add the parens after the observables, since you are evaluating them.

在 observables 之后添加括号,因为您正在评估它们。

<input type="checkbox" data-bind="checked:displayDeactive"> Display deactive</input>
<br/><br/>
<table>
    <tbody data-bind="foreach: products">
        <tr data-bind="visible: active() || $parent.displayDeactive()">
            <td><span data-bind="text:name"></span></td>
        </tr>
    </tbody>
</table>

You can find the full code here: http://jsfiddle.net/johnpapa/gsnUs/

你可以在这里找到完整的代码:http: //jsfiddle.net/johnpapa/gsnUs/

You could use a computed property on the templated item that evaluates the expression (just saw the @RPNiemeyer responded with that too ... I +1'd).

您可以在评估表达式的模板项上使用计算属性(刚刚看到@RPNiemeyer 也做出了回应......我+1'd)。