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

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

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

我正在为 Udacity 前端 Web 开发纳米学位开发一个社区地图项目。这个想法是使用 google 地图 api 来显示一个城市,并用不同位置的标记填充该地图,以及所有位置的可过滤列表。可过滤列表中的位置应该是可点击的,并导致在与被点击位置对应的地图标记上发生一些动作。

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

Uncaught TypeError: Cannot read property 'infowindow' of undefined _____self.openInfoWindow @ app.js:95
_____(anonymous function) @ knockout-3.1.0.js:67

Here is a link to my project in 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);

(点前面没有"self.")

此外,您的 "point" class 没有显示信息窗口。您需要在 https://github.com/Caoimhin89/Udacity_Project_5/blob/master/app.js#L21

上执行 this.infowindow 而不是 var infowindow