我在我的单页应用程序中使用knockoutjs,我目前陷入了一个神秘的问题.
我试图显示一个下拉菜单,并使用敲除绑定填充它.为此,我使用了一个遍历所有元素的foreach:
<div data-bind="foreach: favPlaces" class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a data-bind="text: name, click: $parent.openInfoWindow" class="dropdown-item">Place Name</a>
</div>
Run Code Online (Sandbox Code Playgroud)
然后,在我的viewModel中,我有openInfoWindow函数(应该在单击下拉项时调用):
// View Model
var TokyoViewModel = function() {
var self = this;
// All the favorite places
this.favPlaces = ko.observableArray([]);
mFavPlaces.forEach(function(place) {
self.favPlaces.push(new FavPlace(place));
});
this.openInfoWindow = function(favPlace) {
console.log("Success!");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我添加click:openInfoWindow绑定到dropdown-item元素时,我收到以下错误:
Uncaught TypeError: Unable to process binding "foreach: function (){return favPlaces }"
Message: Unable to process binding "click: function (){return $parent.openInfoWindow }"
Message: u(...).bind is not a function
at Object.p (knockout-3.4.1.js:17)
at …Run Code Online (Sandbox Code Playgroud) javascript onclick knockout-mapping-plugin knockout-2.0 knockout.js