Jag
Jag

Reputation: 1870

Knockout.js 1.3.0 IF statement syntax when using containerless control flow

Here's example from Steve Sanderson's blog demonstrating a containerless IF statement in knockout:

<h3>Products</h3>

<ul>
    <li><strong>Here is a static header item</strong></li>
    <!-- ko foreach: products -->
    <li>
        <em data-bind="text: name"></em>
        <!-- ko if: manufacturer -->        
            &mdash; made by <span data-bind="text: manufacturer.company"></span>
        <!-- /ko -->
    </li>
    <!-- /ko -->
</ul>

How would I make the IF statement more complex. I am trying the following and it doesn't work (always returns false):

<!-- ko if: PlanStateName == 'Draft' -->
<div>This plan is a draft!</div>
<!-- /ko -->

How would one accomplish this?

Upvotes: 1

Views: 5566

Answers (2)

Jag
Jag

Reputation: 1870

So it turns out I made a rookie mistake. Here's the working code:

<!-- ko if: PlanStateName() == 'Draft' -->
<div>This plan is a draft!</div>
<!-- /ko -->

Since the variables are wrappered by knockout, the parentheses on PlanStateName are required to access the underlying data.

Upvotes: 5

photo_tom
photo_tom

Reputation: 7342

You need to surround the logical statement with {}.

See http://jsfiddle.net/photo_tom/nvYdf/55/

Upvotes: 0

Related Questions