Question

I have this error

Unable to parse bindings.
Message: TypeError: Cannot read property 'length' of undefined;
Bindings value: text:SomeArray().length

I have recreated the error in here.

http://jsfiddle.net/LkqTU/12758/

var somevalue = [{Test:[]}]; // this is not always blank, but i cannot control it.
var ViewModel = function() {
     this.SomeArray=ko.observableArray(somevalue.Test);   
};

<span data-bind="text:SomeArray().length"></span>
<!-- ko if: SomeArray().length>0 -->
Hello World!
<!-- /ko -->

using

<!-- ko if: SomeArray() -->
Hello World!
<!-- /ko -->

doesn't creates the error but also doesn't skip the "Hello World" Message when the array is blank. i need to check if the array is blank and display the message only if not blank.

I can't use foreach because i am not looping though the array but just checking if it is blank only.

Was it helpful?

Solution

somevalue is an array. So somevalue.Test is not defined but somevalue[0].Test will return [].

this.SomeArray=ko.observableArray(somevalue[0].Test);   

See fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top