繁体   English   中英

如何对在Knockout中接受参数的方法进行数据绑定?

[英]How can I data-bind a method that takes an argument in Knockout?

我试图将当前对象作为参数传递给我的视图模型中的方法,但是由于某种原因,我不断收到错误消息,提示该对象未定义。

我正在为Udacity前端Web开发纳米学位进行邻里地图项目。 这个想法是使用google maps api来显示城市,并使用各种位置的标记来填充该地图,并具有所有位置的可过滤列表。 可过滤列表中的位置应该是可单击的,并且会在与要单击的位置相对应的地图标记上执行某些操作。

我的问题是,当我单击列表中的位置之一时,出现以下错误:

未捕获的TypeError:无法读取未定义_____ self.openInfoWindow @ app.js:95的属性'infowindow'
_____(匿名函数)@淘汰赛3.1.0.js:67

这是我在Github中的项目的链接。
以下是相关的代码片段,以供快速参考:

在HTML中:

<tbody data-bind="foreach: {data: filterPoints, as: 'point'}">
  <tr>
    <td>
      <a href="#" data-bind="text: name, click: $parent.openInfoWindow"></a>
    </td>
  </tr>
</tbody>

在JS文件中:

//create filter function so user can narrow the number of points in the list and on the map

      self.filterPoints = ko.computed(function() {
        var search = self.query().toLowerCase();
        return ko.utils.arrayFilter(self.points(), function(point) {
          var doesMatch = point.name().toLowerCase().indexOf(search) >= 0;
          point.isVisible(doesMatch);
          return doesMatch;
        });
      });

//create Google Maps infowindows for each point on the map

      self.openInfoWindow = function(point) {
        self.point.infowindow.open(map, point.marker);
        console.log(point);
      };

预先感谢任何可以帮助我的人!

在GitHub存储库中,您有一个self.points可观察数组,但是没有self.point字段或可观察对象。 我怀疑你的意思

point.infowindow.open(map, point.marker);
console.log(point);

(前面没有“自我”。)

另外,您的“ point”类没有暴露信息窗口。 您需要在https://github.com/Caoimhin89/Udacity_Project_5/blob/master/app.js#L21上执行this.infowindow而不是var infowindow

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM