0

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.

1
  • 1
    It should be <!-- ko if: numbers()[1].value() > 1 --> Commented May 20, 2015 at 10:16

3 Answers 3

3

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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 -->

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.