Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using logical operator in knockout conditional if binding

<!-- ko if:name == 'Setup' || name == 'Appeals' -->
  <button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret">/span>/button>
<!-- /ko -->

I am trying to show a button with a dropdown only when the name property contains the value "Setup" or "Appeals" but it does not work. Can we use logical operator in above knockout if binding ?

like image 210
user2585299 Avatar asked Oct 14 '13 13:10

user2585299


People also ask

How do you write if condition in knockout?

If the condition evaluates to true or true-like value, then the given HTML markup will be processed. Else, it will be removed from DOM. If the condition in the parameter contains an observable value, then the condition is re-evaluated whenever the observable value changes.

What is binding in knockout js?

A binding context is an object that holds data that you can reference from your bindings. While applying bindings, Knockout automatically creates and manages a hierarchy of binding contexts. The root level of the hierarchy refers to the viewModel parameter you supplied to ko. applyBindings(viewModel) .

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 you are using a conditional statement inside of your binding remember that you need to 'get' the value -

<!-- ko if: name() === 'Setup' || name() === 'Appeals' -->
  <button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret">/span>/button>
<!-- /ko -->
like image 180
PW Kad Avatar answered Sep 21 '22 04:09

PW Kad