【问题标题】:Geting value of the input that is clicked through knockout获取通过敲除单击的输入的值
【发布时间】:2017-06-18 10:45:54
【问题描述】:

我对淘汰赛还很陌生,所以我想弄清楚如何让我的小应用程序正常工作。本质上,我希望谷歌地图在点击时从我的列表中放大到一个地方。谷歌地图部分正在工作。我创建了一个使用淘汰赛 foreach 方法填写的列表。然后,我将项目的值绑定到我的函数,但它一直抛出错误:

"knockout-3.4.2.js:1871 Uncaught ReferenceError: Unable to process binding "foreach: function (){return places }" 消息:无法处理绑定“点击:函数(){return 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"));

谢谢,您的意见将不胜感激!

【问题讨论】:

    标签: javascript jquery google-maps knockout.js


    【解决方案1】:

    zoomToPlace 更改为$parent.zoomToPlace

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

    【讨论】:

    • 我无法表达我对你的感激之情!成功了!
    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2015-12-02
    相关资源
    最近更新 更多