how to display array element?

64 views
Skip to first unread message

Artem -

unread,
Mar 19, 2017, 3:51:22 PM3/19/17
to KnockoutJS


I have some array and this code to initialize ko object
var PointsArray = [
{...},
{...},
{...}
];


$(function() {
    ...
    ko.applyBindings(new PointsViewModel());
});

function PointsViewModel(){
   
var self = this;
   
self.points = ko.observableArray(PointsArray);
   
//this.chosenPoint = ko.observable();
   
self.chosenPoint = self.points()[document.getElementById('eCurrentPlacemarkId').value];
}
</script>

<body>
<input type="hidden" id="eCurrentPlacemarkId" value="1">
<input type="text" id="eCurrentPlacemarkName" data-bind="value: chosenPoint.name">

I`d like to display some array element but it does not work, nothing happens :((
How can i make it work?
Message has been deleted

noirabys

unread,
Mar 20, 2017, 2:48:11 AM3/20/17
to KnockoutJS
hi, 

1. call applyBindings after dom has been created

$(document).ready(function() {
    ...
    ko.applyBindings(new PointsViewModel());
});

2. do not reference dom in model 

<div data-bind="foreach:points">
  <div data-bind="text: $data, click; chosenPoint"></div> // click call method with $data as first argument, $data in foreach ist the current item
</div>

best regards,
 noirabys

you may have a look at the beginners tutorial on the knockoutjs page
Reply all
Reply to author
Forward
0 new messages