The category
variable is defined as ko.observable()
and child value should be changed according to category
value.
Below if statement always returns false.
<!-- ko if: $parent.category == "Electronics"-->
<div>abc</div>
<!--/ko-->
the if satement below also always returns false:
<span data-bind="if: $parent.category == 'Electronics'">
<div>abc</div>
</span>
How to do comparison in data-binding with hard-code values?
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.
Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding.
KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites.
KO observables are functions and to get their value inside expressions you need to call them without any argument e.g.: $parent.category()
So you need to change your code to:
<!-- ko if: $parent.category() == "Electronics"-->
<div>abc</div>
<!--/ko-->
In your original code you are comparing the observable function ($parent.category
) and not its value with "Electronics"
that is why its always false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With