Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout.js "if Binding" on multiple booleans

Is it possible to use Knockout's if binding on more than one boolean?

Such as

<div data-bind="if: (property.aTrueValue && property.anotherTrueValue)">... 

I've tried a lot of different syntax, but can't seem to find the right syntax. I'm not sure it's even possible.

like image 332
Brad Bamford Avatar asked Mar 09 '13 05:03

Brad Bamford


People also ask

Can we have multiple knockout model?

Knockout now supports multiple model binding. The ko. applyBindings() method takes an optional parameter - the element and its descendants to which the binding will be activated. This restricts the activation to the element with ID someElementId and its descendants.

What is two-way binding in knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.


1 Answers

When Knockout processes your bindings it first evaluates your expression.

If the expression results in an observable, it then evaluates the observable as a convenience to get the final value that the if: works on.

So the two following work identically

<div data-bind="if: foo"></div> <div data-bind="if: foo()"></div> 

Once you leave the world of simple expressions ending in an observable, you probably also want to leave the sugar behind and always evaluate the observables yourself (for sanity if nothing else).

Try the following

<div data-bind="if: (property.aTrueValue() && property.anotherTrueValue())">... 
like image 166
Adam Kennedy Avatar answered Sep 21 '22 01:09

Adam Kennedy