2017-04-17 97 views
0

我无法在此特定页面上使用$ root访问方法。此代码适用于我的其他页面,但我不明白。我每页有一个viewmodel。它无法找到removeAttachment。敲除js根绑定

淘汰赛3.4.0.js:72遗漏的类型错误:无法处理绑定 “点击:函数(){$返回data.bind.removeAttachment($的数据,事件,$指数)}” 消息:无法读取未定义

var model = function AppViewModel(){ 
self.removeAttachment = function(data, event, attachmentClicked){ 
    fileNameToDelete = attachmentClicked; 
    $("deleteText").append(" " + attachmentClicked + "?"); 
    $('#delete-confirm').modal('show'); 
}; 
}; 
var app = new model(); 
ko.applyBindings(app, document.getElementById("panel")); 
<div id="panel"> 

<tbody class="types"> 
<!-- ko foreach: multiFileData().fileArray --> 
<tr> 
<td><span class="attachName" data-bind="text:name"></span></td> 
<td><span class="attachName" data-bind="$parent.sizeInMB: size"></span></td> 
<td id="remove" class="glyphicon glyphicon-trash" data-toggle="modal" data-target="delete-confirm" 
data-bind="click:$root.bind.removeAttachment($data, event, $index)"> </td> 
</tr> 
<!-- /ko --> 
</tbody> 
</div> 
财产 'removeAttachment'

回答

1

你可能想:

click: $root.removeAttachment 

如果需要额外的参数传递:

click: $root.removeAttachment.bind($root, $index) 

传递给函数的第一个和第二个参数始终为$dataevent。使用bind,您可以将它们推回并传入新的第一个参数(以及设置this值)。

此外,您还需要确保removeAttachment实际上设置在视图模型中。

var model = function AppViewModel() { 
    var self = this; 
    self.removeAttachment = ... 
}; 
+0

试图这样:不能与$ root.removeAttachment一起工作 – Hardrock302

+0

我扩展了我的答案, –