我只是從knockout.js參考教程:
http://learn.knockoutjs.com/#/?tutorial=webmail
在UI的標記是:
<!-- Folders -->
<ul class="folders" data-bind="foreach: folders">
<li data-bind="text: $data,
css: { selected: $data == $root.chosenFolderId() },
click: $root.goToFolder"></li>
</ul>
,它的視圖模型是:
function WebmailViewModel() {
// Data
var self = this;
self.folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
self.chosenFolderId = ko.observable();
// Behaviours
self.goToFolder = function(folder) { self.chosenFolderId(folder); };
};
ko.applyBindings(new WebmailViewModel());
能
人告訴我什麼是$ root,爲什麼它是必需的?如果我刪除它,它不起作用。
你的回答對我有幫助,+ 1 – SpringLearner