Reputation: 93
I am using the ko if
statement in my html view, which refers to a knockout observableArray.
You can see my code on jsfiddle.
I am curious why this line of code is not working?
<!-- ko if: numbers()[1].value > 1 -->
If I print out the value
, I am getting the value that I expected.
Upvotes: 0
Views: 686
Reputation: 7641
You should use brackets after "value", because the "value" is an observable and you want to use its content:
<!-- ko if: numbers()[1].value() > 1 -->
Upvotes: 1
Reputation: 7110
It should be <!-- ko if: numbers()[1].value() > 1 -->
since numbers()[1].value
is an observable (every observable is a function).
In the above case when if: numbers()[1].value > 1
is considered, you are comparing a function with 1 (which is always false).
Upvotes: 3
Reputation: 169
Here's one way
<h4>Numbers</h4>
<button data-bind="click: add">Add</button>
<input id="myid" data-bind="value: numbers()[1].value"></input>
<!-- ko foreach: numbers -->
<!-- ko if: $index() == 1-->
<div data-bind="text: value">
</div>
<!-- /ko -->
<!--/ko-->
<span data-bind="text: sum"></span>
Upvotes: 0