微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-Knockout.js-获取错误-未捕获ReferenceError:无法处理“ with:function”绑定

我正在做一个邻里地图项目,被卡住了!我是基因敲除的新手.我正在尝试使用数据绑定得到此错误-

Knockout-3.4.1.js:72未捕获ReferenceError:无法处理绑定“ with:function(){returnfilteredItems}”

HTML代码的片段-

section class="main">
          <form class="search" method="post" action="index.html" >
            <input type="text" data-bind="textInput: filter" placeholder="Click here/Type the name of the place">
            <ul data-bind="with: filteredItems">
              <li><span data-bind="text: title, click: $parent.showInfoWindow"></span></li>
            </ul>
         </form>
        </section>

这是我的viewmodel-

function viewmodel(markers) {
  var self = this;
  self.filter = ko.observable(''); // this is for the search Box, takes value in it and searches for it in the array
  self.items = ko.observableArray(locations); // we have made the array of locations into a ko.observableArray
  // attributed to - http://www.knockmeout.net/2011/04/utility-functions-in-knockoutjs.html , filtering through array
  self.filteredItems = ko.computed(function() {
    var filter = self.filter().toLowerCase();
    if (!filter) {
      return self.items();
    } else {
      return ko.utils.arrayFilter(self.items(), function(id) {
        return stringStartsWith(id.name.toLowerCase(), self.filter);
      });
    }

  });

  var stringStartsWith = function (string, startsWith) {
       string = string || "";
       if (startsWith.length > string.length)
           return false;
       return string.substring(0, startsWith.length) === startsWith;
   };
  // populateInfoWindow(self.filteredItems,)


  // this.showInfoWindow = function(place) { // this should show the infowindow if any place on the list is clicked
  //     google.maps.event.trigger(place.marker, 'click');
  // };

}

一些行被注释,因为我还在努力.看整个项目-https://github.com/Krishna-D-Sahoo/frontend-nanodegree-neighborhood-map

解决方法:

with绑定使用提供的元素创建新的绑定上下文.由于对< span>中的标题的引用而引发该错误.元素,但filteredItems没有标题属性.

如果要显示< li>对于filteredItems数组中每个元素的元素,可以使用foreach绑定,如下所示:

<ul data-bind="foreach: filteredItems">
  <li><span data-bind="text: title, click: $parent.showInfoWindow"></span></li>
</ul>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐