RaulMonteroC RaulMonteroC - 1 month ago 4
Javascript Question

how to bind knockout's Observable array length to an html element

Supose I have the following viewmodel

var ViewModel = function() {
this.fruits = ko.observableArray(["Apple", "banana", "orange"]);
ko.applyBindings(new ViewModel());

I want to be able to do something like this to bind the observable array's length on the html like any other viewmodel property.

<span data-bind="text: fruits.length"></span>

For this purpuse, I tried converting observable array to simple array, but with no success

span data-bind="text: fruits().length"></span>

I understand this should be easy and therefore must be missing something. Could you please point me in the right direction.


Not sure what problem you're having but this works fine:

<span data-bind="text: fruits().length"></span>

Working example