Knockout attr binding with attributes like 'readonly' and 'disabled'

前端 未结 3 1512
粉色の甜心
粉色の甜心 2020-12-06 16:36

What\'s the suggested \"best practice\" way to use Knockout\'s \"attr\" data binding with standalone attributes like \"readonly\" and

3条回答
  •  既然无缘
    2020-12-06 16:55

    You can also create a binding for readonly like this:

    ko.bindingHandlers['readonly'] = {
    'update': function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (!value && element.readOnly)
            element.readOnly = false;
        else if (value && !element.readOnly)
            element.readOnly = true;
    }
    };
    

    Source: https://github.com/knockout/knockout/issues/1100

提交回复
热议问题