如何设置ko.bindingHandlers值

时间:2014-03-22 01:18:06

标签: knockout.js

ko.bindingHandlers.translate = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var translateTo = bindingContext.$data.domain._latestValue;
            console.log(translateTo);// output : work
            //my wuestion is how can i set this in html
           }
    };

HTML:

 <span data-bind="translate: domain"></span><!--this is not working-->

enter image description here

1 个答案:

答案 0 :(得分:1)

你真正想要填写的是&#34;更新&#34;方法,并直接从valueAccessor访问模型。您不需要使用bindingContext,因为您的绑定非常简单。

ko.bindingHandlers.translate = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        var domain = ko.unwrap(valueAccessor());
        var translateTo = ko.unwrap(domain._latestValue);
        element.textContent = translateTo;
    }
};