2017-06-18 57 views
2

我是淘汰赛的新手,所以我想弄清楚如何让我的小应用程序工作。实质上,我希望Google地图可以放大点击列表中的某个地点。 Google地图部分正在工作。我创建了一个我使用knockout foreach方法填写的列表。然后,我已将项目的值绑定到我的函数,但它一直抛出错误:获得通过淘汰赛点击的输入的价值

“knockout-3.4.2.js:1871 Uncaught ReferenceError:Unable to process binding”foreach:function(){return places} “ 消息:无法处理绑定 ”点击:函数(){返回zoomToPlace}“ 消息:zoomToPlace没有定义”

这里是我的这一部分HTML:

<div class="scroll" id="myUL"> 
    <ul data-bind="foreach: places"> 
    <li> 
     <input data-bind="value: title, click: zoomToPlace" type="button" class="btn btn-small btn-link"> 
    </li> 
    </ul> 
</div> 

这里是我的js :

function PlacesList() { 
var self = this; 
self.places = ko.observableArray(locations); 
self.title = ko.observable(); 
self.zoomToPlace = function() { 
    // Initialize the geocoder. 
    var geocoder = new google.maps.Geocoder(); 
    // Get the place. 
    var address = this.title(); 
    // Geocode the address/area entered to get the center. Then, center the map on it and zoom in 
    geocoder.geocode({ 
     address: address, 
     componentRestrictions: { 
      locality: 'North York' 
     } 
    }, function(results, status) { 
     map.setCenter(results[0].geometry.location); 
     map.setZoom(15); 
    }); 
} 
} 

ko.applyBindings(new PlacesList(), document.getElementById("myUL")); 

谢谢您的意见!

回答

0

zoomToPlace更改为$parent.zoomToPlace

forEach循环内,上下文是当前的places项目。由于zoomToPlace不是places中的项目的方法,因此您收到错误消息。

+1

我无法表达我多么感激你!它已经工作了! –

+0

太棒了!真高兴你做到了 – DonovanM