- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 knockout.js 与 MVC3 一起使用,但我一直收到错误消息:
Uncaught TypeError: Cannot call method 'remove' of undefined
设置是我有一个 UL 列表,我需要从中添加和删除:
<ul data-bind="foreach: Interviewees">
<li>
<div>
<a data-bind="click: $root.removeInterviewee" class="xOut"></a>
</div>
<div>
<h2>
<span data-bind="text: FirstName"></span>
<span data-bind="text: LastName"></span>
</h2>
</div>
</li>
</ul>
这是具有 knockout 内容的 javascript 部分:
function SomeThingee(Id, SomeThingeeId, firstName, lastName, title, email) {
this.Id = Id;
this.SomeThingeeId = SomeThingeeId;
this.FirstName = firstName;
this.LastName = lastName;
this.Title = title;
this.Email = email;
}
var viewModel = ko.validatedObservable({
addSomeThingee: function () {
if (!viewModel.isValid()) {
viewModel.errors.showAllMessages();
return false;
} else {
var newSomeThingee = new SomeThingee(this.Id(), 0, this.FirstName(), this.LastName(), this.Title(), this.Email());
$.ajax({
url: '@Url.Action("AddSomeThingee")',
type: "POST",
data: ko.toJSON(newSomeThingee),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
newSomeThingee.SomeThingeeId = result.message;
},
error: function (result) {
}
});
this.SomeThingees.push(newSomeThingee);
}
},
removeSomeThingee: function (item) {
this.SomeThingees.remove(item);
}
});
$(function () {
var jsonModel = '@Html.Raw(JsonConvert.SerializeObject(this.Model))';
var mvcModel = ko.mapping.fromJSON(jsonModel);
var myViewModel = new viewModel();
var g = ko.mapping.fromJS(myViewModel, mvcModel);
ko.applyBindings(g, document.getElementById("someDiv"));
});
错误发生在这一行:
this.SomeThingees.remove(item);
请注意,SomeThingees 集合由模型本身提供。 add 方法工作得很好,但是 remove 方法不起作用并给我上面列出的错误。我做错了什么?
最佳答案
问题是当 click
绑定(bind)调用 $root.removeInterviewee
时,this
被设置为数据项( $data
) 而不是 View 模型 ($root
)。有几种方法可以解决这个问题。可能最简单的方法是对绑定(bind)中的函数引用使用 bind
。
<a data-bind="click: $root.removeInterviewee.bind($root)" class="xOut"></a>
另见 Github issue进一步讨论。
关于asp.net-mvc-3 - 未捕获的类型错误 : Cannot call method 'remove' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247354/
我正在使用 .remove() 方法删除一个 html 元素,同时对于这个元素,我有一个事件处理程序,但它没有被触发。为什么会这样呢?这是jsFiddle和代码:HTML Delete I'm goi
所以我尝试从另一篇文章中编写此代码: while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
我正在阅读 Nicolai M.Josuttis 撰写的“The C++ STL. A Tutorial and References”一书,在专门介绍 STL 算法的一章中,作者陈述如下:如果你调用
是否有一种简单的机制来确定 DownloadManager remove() 何时完成,因为它看起来是部分异步的。该函数几乎立即返回下载表中已删除的条目计数,但实际的文件系统管理似乎被插入了某个后台线
我愿意: getActionBarToolbar().removeView(logoImage); getActionBarToolbar().addView(logoImage, lp); 我得到:
我有类(class)评论一对多关系。在类(class)表中有 id 和 title 列。在 Review 表中,有 id、comment 和 course_id,其中“course_id”作为指向类(
我在 stackoverflow 上阅读了不同的答案,了解如何销毁 wigdet/jQueryObject 并取消绑定(bind)其上的所有事件。 这就是我的想法。 $('选择器').remove()
我有一个由一个线程填充的 byte[] 列表,然后我有另一个线程正在从该列表中读取并通过网络发送项目。 每次我读取线程 2 中的项目时,我都想将其从内存中清除。但是因为我正在使用线程,如果我使用 .r
就算法而言,从连续数组中删除一组元素可以分两部分有效地完成。 将所有不删除的元素移到数组的前面。 将数组标记得更小。 这可以在 C++ 中使用 erase-remove 习惯用法来完成。 vector
我尝试删除包含在 map 中渲染的制造商的 View 。当我单击按钮时,事件 click .ver 被激活,但没有任何反应,并且我收到以下错误:Uncaught TypeError: undefine
场景: 使用 jQuery 2.0.1 构建的应用程序。 您的团队更喜欢原生 JavaScript。 选项有jQuery .remove()和 ChildNode.remove() . 您需要删除节点
最初我有一个像这样的删除功能: function ViewWorkflowDetail(btn, workflowId) { $("#workflowDetailPanel").remov
我正在编写 C++ 代码来解决 Leetcode 中的这个问题:https://leetcode.com/problems/remove-element/ Given an array nums an
根据太阳, "Iterator.remove is the only safe way to modify a collection during iteration; the behavior is
众所周知,从 std::vector 中完全删除所需项的一种好方法是 erase-remove idiom . 如以上链接中所述(截至本文发布日期),在代码中,erase-remove 习惯用法如下所
我在 HashSet 上调用 Iterator.remove() 时遇到问题。 我有一组带有时间戳的对象。在将新项目添加到集合之前,我会遍历集合,识别该数据对象的旧版本并将其删除(在添加新对象之前)。
这段代码: Collection col = new ArrayList(); col.add("a"); col.add("b"); col.add("c");
我试图通过在下面输入来卸载 conda 环境基础, conda env remove -n base 正如我所建议的那样,我尝试通过使用来停用基地 conda deactivate base 我再次尝
我已经对我的 IOS 应用程序进行了质量扫描分析。我收到以下警告: The binary has Runpath Search Path (@rpath) set. In certain cases
这个问题已经有答案了: Properly removing an Integer from a List (8 个回答) 已关闭 4 年前。 我是java新手。看起来很简单,但我不明白为什么会发生这种
我是一名优秀的程序员,十分优秀!