Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

knockout.js - How to get an observableArray's length?

I am attempting to create an array of contract line items (CLINs) that will be displayed as individual div elements below a header of general contract information.

I am able to get the normal observables to work, but it appears that the passing of the array via the constructor for the view model is not creating any part of the clins observable array.

I have a jsFiddle that illustrates my problem. What is strange to me is that the data-bind="text: clins.length() on the HTML span tag does not even return zero, but instead renders nothing.

Is there anyway to enable debugging within a jsFiddle or should I see a warning/error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Errors from jsfiddle pages do get sent to your browser.

As for your error, try this:

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

This turns the observableArray into an array and uses the array's length property.

See the updated the jsfiddle as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...