Pergunta

happy new year, and I have the following issue with knockoutjs if statement.

I have the following

  z.object = ko.observableArray(ko.utils.arrayMap([{"Id":3,"Date":"2014"}]))

  <div data-bind="text: Date"></div>

above prints fine, 2014 but below if statement does not work.. what is going on?

 <!-- ko if: Date === 2014 -->
 <!-- /ko -->

I am totally baffled by this.

Foi útil?

Solução

From SO answer here:

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

So in your statement Date === 2014, here Date is tring and 2014 is number, then result is false.

Either convert string to number, number to string or use == instead of ===.

Outras dicas

Simplified example seems okay to me with ==, but not ===, please see this fiddle:

http://jsfiddle.net/cr2QT/2/

<div data-bind="text: Date"></div>

<hr>
<!-- ko if: Date == 2014 -->
<h1>Oogah Chaka (==)</h1>
<!-- /ko -->

<hr>
<!-- ko if: Date === 2014 -->
<h1>Chaka (===)</h1>
<!-- /ko -->

var viewModel = {
  Date: "2014"
}
ko.applyBindings(viewModel);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top