문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top