gpt4 book ai didi

c# - knockout View 模型+所选项目->嵌套列表 "not defined"

转载 作者:行者123 更新时间:2023-12-03 11:49:59 24 4
gpt4 key购买 nike

我有一个 View 模型

var viewModel = {
Classes: ko.observableArray(data),
addPerson: function() {
SelectedClass.Persons.push(new Person(SelectedClass, " ", " "));
},
SelectedClass: ko.observable({
Persons: ko.observableArray([]),
Location: ko.observable("")
}),
};
ko.applyBindings(viewModel);

我将主从 View 与类绑定(bind)。选择一个类(class)以获取该类(class)中的“人员”列表。

我有一个按钮可以将一个人添加到类(class)中。当我尝试单击它时,我得到:

ReferenceError:未定义人员。

为什么它无法找到 SelectedClass 中的 Person 嵌套列表并推送到该列表?

我用一些测试数据创建了一个 fiddle 。 http://jsfiddle.net/alexintime/n77ujya3/2/尽管在 fiddle 中我无法让显示详细信息按钮起作用,因此无法填充“SelectedClass”。但至少它是一些东西。

提前致谢

最佳答案

在 JavaScript 中,从整个对象的成员对对象属性的任何访问都应该使用 this 进行限定(这不是 C# 的情况,在 C# 中,很少有边缘情况需要您显式使用 this):

// Check that you need to get "SelectedClass" property value like a function call 
// (Knockout's getter function):
addPerson: function() {
this.SelectedClass().Persons.push(new Person(this.SelectedClass(), " ", " "));
}

更新,问题#2

this.SelectedClass 是一个可观察的属性。为了访问 Persons 属性,您需要像函数一样获取 SelectedClass 属性值:this.SelectedClass()!

关于c# - knockout View 模型+所选项目->嵌套列表 "not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25886183/

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