Knockout.js可观察通知'始终'并订阅

时间:2015-05-31 10:42:28

标签: knockout.js

我有以下代码:

HTML:

<p>Age:
    <input data-bind="value:age" />
</p>
<p>Age has been changed: <span data-bind="text:ageChangeCount"></span> times</p>

JS:

function viewModel() {

    var self = this;

    self.age = ko.observable(10).extend({
        notify: 'always'
    }), 

    self.ageChangeCount = ko.observable(0),

    self.age.subscribe(function (val) {
        console.log('Someone has changed age');
        self.ageChangeCount(self.ageChangeCount() + 1);
    });

};

ko.applyBindings(new viewModel());

JSFiddle:http://jsfiddle.net/EstSiim/9majtx0h/

我想知道为什么ageChangeCount在老年和新年龄相同时(输入失去焦点时)不会增加?

1 个答案:

答案 0 :(得分:1)

因为当新值与旧值匹配时,value绑定处理程序只是忽略模型更新。因此,订阅也不会运行。

请参阅related question