在 knockout 中,在接收新值之前,是否可以在该可观察值的订阅中获取该可观察值的当前值?
示例:
this.myObservable = ko.observable();
this.myObservable.subscribe(function(newValue){
//I'd like to get the previous value of 'myObservable' here before it's set to newValue
});
最佳答案
ko.subscribable.fn.subscribeChanged = function (callback) {
var oldValue;
this.subscribe(function (_oldValue) {
oldValue = _oldValue;
}, this, 'beforeChange');
this.subscribe(function (newValue) {
callback(newValue, oldValue);
});
};
像这样使用上面的内容:
MyViewModel.MyObservableProperty.subscribeChanged(function (newValue, oldValue) {
});
关于knockout.js - 在订阅同一可观察值时获取可观察值的前一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12822954/