2017-04-17 96 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

我擴展了我的答案, –