我举了一个例子,说明我要做的事情jsfiddle
我是淘汰赛的新手,从昨天开始就开始研究它。所以当我运行代码时出现这个错误。
Uncaught ReferenceError: Unable to process binding "foreach: function (){return itemcarousel }"
Message: Unable to process binding "text: function (){return markerNumber }"
Message: markerNumber is not defined
以下是我目前使用的代码:
// Sends the latitude and longitude to the controller to find closes franchises
$.ajax({
url: '{{ URL::action('frontend_getLocation') }}',
type: 'POST',
dataType: 'JSON',
data: {'latitud': lat, 'longitud': lgn}
}).done(function(data) {
//The order of elements in the array id=0, distance=1, region=2, town=3, address=4, postalcode=5, phone=6
var itemCarouselList = [];
for(var i = 0; i < data.length; i++) {
var temp = new fillingData(
i+1,
data[i][1],
data[i][3],
data[i][4],
data[i][2],
data[i][5],
data[i][6] );
console.log(temp);
itemCarouselList.push(temp);
}
console.log(itemCarouselList);
var viewModel = {
itemcarousel: ko.observableArray([ itemCarouselList ])
};
ko.applyBindings(viewModel);
});
function fillingData(markerNum, km, kotown, koaddress, koregion, kopostalcode, kophone) {
var self = this;
self.markerNumber = markerNum;
self.kilometers = km;
self.town = kotown;
self.address = koaddress;
self.region = koregion;
self.postalcode = kopostalcode;
self.phone = kophone;
}
答案 0 :(得分:1)
问题在于初始化observableArray的方式。它只接受并且你不需要将它包装在[]另一个数组
中var viewModel = {
people: ko.observableArray(
itemCarouselList
//new fillingData(33, 300), //{ markerNumber: 33, kilometers: 300 },
//new fillingData(12, 666) //{ markerNumber: 32, kilometers: 400 }
)
};