单个元素上相同类型的两个自定义绑定
我有以下代码:
ko.bindingHandlers.myBinding = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
alert(value);
}
};
ko.applyBindings({
firstValue: ko.observable(true),
secondValue: ko.observable(false)
});
<input data-bind="myBinding: firstValue, myBinding: secondValue"/>
运行此代码只会产生一个带有“false”的警报框。是否有一个设置可以用来获得预期的行为,或者我的选择是否仅限于此:
var temp = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
alert(value);
}
};
ko.bindingHandlers.myBinding = temp;
ko.bindingHandlers.myBinding2 = temp;
ko.applyBindings({
firstValue: ko.observable(true),
secondValue: ko.observable(false)
});
<input data-bind="myBinding: firstValue, myBinding2: secondValue"/>
谢谢
I have the following code:
ko.bindingHandlers.myBinding = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
alert(value);
}
};
ko.applyBindings({
firstValue: ko.observable(true),
secondValue: ko.observable(false)
});
<input data-bind="myBinding: firstValue, myBinding: secondValue"/>
Running this yields only one alert box with "false". Is there a setting that I can utilize to get the expected behavior, or are my options limited to doing this:
var temp = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
alert(value);
}
};
ko.bindingHandlers.myBinding = temp;
ko.bindingHandlers.myBinding2 = temp;
ko.applyBindings({
firstValue: ko.observable(true),
secondValue: ko.observable(false)
});
<input data-bind="myBinding: firstValue, myBinding2: secondValue"/>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在同一元素上使用两个相同的绑定,因为它们是内部键控的。
您需要创建一个接受这两个值或值数组的绑定,例如:
data-bind="myBinding: [firstValue, secondaryValue]"
然后对每个值进行运算符。
否则,您可以按照您的建议进行操作,并创建多个指向同一函数的绑定。
You can't use two of the same bindings on the same element, as they are keyed internally.
You would need to either create a binding that accepts both values or an array of values like:
data-bind="myBinding: [firstValue, secondValue]"
Then operator on each one.
Otherwise, you could do what you are suggesting and create more than one binding that points to the same function.