gpt4 book ai didi

javascript - 过滤器与 knockout.js 绑定(bind)的问题

转载 作者:行者123 更新时间:2023-12-03 04:08:49 25 4
gpt4 key购买 nike

我是新使用 Knockout.js 框架的人,我的代码中出现以下错误,我正在使用 Knockout.js 构建应用程序,一切正常,除了当我尝试创建 dependentObservable 时。

这是 JavaScript 代码:

(function() {
'use strict';

console.log("This is my Application");

var app = {
mapElement: document.getElementById('map'),
mapObj: map,
locations : ko.observableArray([
{id: 1, title: 'Holywood Theater', location: {lat: 43.098344, lng: -76.145697}},
{id: 2, title: 'Mattydale Fire Department', location: {lat: 43.098172, lng: -76.142189}},
{id: 3, title: 'Original Italian Pizza', location: {lat: 43.098854, lng: -76.144700}},
{id: 4, title: 'Roxboro Road Middle School', location: {lat: 43.101110, lng: -76.150901}},
{id: 5, title: 'Big Lots', location: {lat: 43.101400, lng: -76.146985}},
{id: 6, title: 'Camnel pub', location: {lat: 43.098670, lng: -76.145832}}
]),
markers:[],
textFilter: ko.observable(),
filterLocations: ko.dependentObservable(function () {
return ko.utils.arrayFilter(app.locations(), function (loc) {
return loc.title().toLowerCase().includes(app.textFilter().toLowerCase());
});
})
};

ko.applyBindings(app);
})();

chrome控制台的错误是:

Uncaught TypeError: Cannot read property 'locations' of undefined

最佳答案

您不能使用“app”var,因为您仍在创建它。

var app = {... 

^^^ return ko.utils.arrayFilter(app.locations(), ...

^^^

关于javascript - 过滤器与 knockout.js 绑定(bind)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com