我正在尝试围绕KO textInput绑定编写一个简单的包装器。输入填充初始值" abc"但是当我在输入中键入任何新值时,相应的observable不会更新。我在这里错过了什么?我对KO很新。
ko.bindingHandlers.myBind = {
init: function(element, valueAccessor, allBindings) {
var test= ko.observable("abc");
test.subscribe(function(newVal){console.log(newVal);});
ko.bindingHandlers.textInput.init(element, test, allBindings);
}
};
ko.applyBindings()
// html
<input data-bind="myBind" />
答案 0 :(得分:1)
valueAccessor
参数必须是函数才能返回observable:
ko.bindingHandlers.myBind = {
init: function(element, valueAccessor, allBindings) {
var test= ko.observable("abc");
test.subscribe(function(newVal){console.log(newVal);});
var newValueAccessor = function(){ return test; };
ko.bindingHandlers.textInput.init(element, newValueAccessor, allBindings);
}
};
ko.applyBindings();
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<input data-bind="myBind" />
&#13;