Yodacheese
Yodacheese

Reputation: 5037

Knockout JS - If bindings inside foreach

Not sure what I am doing wrong but here is my code.

<ul data-bind="foreach: publications" >
   <!-- ko if: mediatype === '2' -->
   <li data-bind="text: mediatype, value: id"></li>
   <!-- /ko -->
</ul>

This is returning the whole list in the foreach (all mediatype numbers)

I also tried this:

<ul data-bind="foreach: publications" >
  <li data-bind="if: mediatype === '2', text: mediatype, value: id"></li>
</ul>

This returns nothing. Not sure what is wrong here?

Upvotes: 2

Views: 1876

Answers (1)

Tom W Hall
Tom W Hall

Reputation: 5283

For an expression such as if: mediatype === '2' you'd need to use brackets i.e. if: mediatype() === '2'

Upvotes: 6

Related Questions