3 回答

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
this
var ViewModel = function(first, last) { this.first = ko.observable(first); this.last = ko.observable(last); this.full = ko.computed(function() { return this.first() + " " + this.last(); }, this);};
this
var viewModel = { first: ko.observable("Bob"), last: ko.observable("Smith"),};viewModel.full = ko.computed(function() { return this.first() + " " + this.last();}, viewModel);
viewModel
viewModel
this
this
var ViewModel = function() { var self = this; this.items = ko.observableArray(); this.removeItem = function(item) { self.items.remove(item); }};
$root.removeItem
this
bind
var ViewModel = function() { this.items = ko.observableArray(); this.removeItem = function(item) { this.items.remove(item); }.bind(this);};

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
var viewModel = (function () { var obj = {}; obj.myVariable = ko.observable(); obj.myComputed = ko.computed(function () { return "hello" + obj.myVariable() }); ko.applyBindings(obj); return obj;})();
不使用 this
,當(dāng)在 ko.computed
S等 我的viewModel是一個(gè)單實(shí)例,我不需要?jiǎng)?chuàng)建多個(gè)實(shí)例。 new viewModel()
)

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
對(duì)于視圖模型這樣的復(fù)雜對(duì)象,我經(jīng)常使用一個(gè)函數(shù)(使用顯示模塊模式)。但是對(duì)于簡(jiǎn)單的模型,我使用一個(gè)函數(shù),這樣我就可以在一個(gè)地方處理所有的事情
添加回答
舉報(bào)