可观察数组和foreach列表

时间:2016-10-27 18:19:00

标签: javascript html knockout.js

我有这段代码:

<select multiple="multiple" data-bind="options:markerResults, optionsText: function(item) {
    return item.name +' '+ item.formatted_address
        }">
</select> 

它有效。但是,这段代码:

<ul data-bind="foreach: markerResults">
     <li>
         <stong><span data-bind:"text: name"></span></strong>
         <span data-bind:"text: formatted_address"></span>
         <span data-bind:"text: rating"></span>
     </li>
</ul>

不。如何使上面的代码工作?

谢谢!

更多代码:

for (var j = 0; j < allResults.length; j++) {
            createMarker(allResults[j]);
            allResults.push(results);
            console.log(allResults);
            }
          }

1 个答案:

答案 0 :(得分:0)

这里是小提琴http://jsfiddle.net/LkqTU/32252/

function model() {
  var self = this;
  this.text = ko.observable('hello');
  this.markerResults = ko.observableArray([
    { name: "Bungle", formatted_address: "1 My Way", rating: 'A' },
    { name: "George", formatted_address: "2 My Way", rating: 'B' },
    { name: "Zippy", formatted_address: "3 My Way", rating: 'C' }
]);

}

var mymodel = new model();

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

这是html

<ul data-bind="foreach: markerResults">
     <li>
      <strong><span data-bind="text: name"></span></strong> 
     <strong><span data-bind="text: formatted_address"></span></strong> 
      <strong><span data-bind="text: rating"></span></strong> 
     </li>
</ul>