是的,
afterRender仅适用于模板绑定.
但是,您可以创建跟踪html绑定更改的自定义绑定处理程序,并在更新值时触发回调.我的例子:
ko.bindingHandlers.afterHtmlRender = {
update: function(el, va, ab){
ab().html && va()(ab().html);
}
}
缩短的参数名称:va – valueAccessor,ab – allBindings.
标记应该看起来像(绑定名称已更改):
更新
带有解释的简化绑定代码:
ko.bindingHandlers.afterHtmlRender = {
update: function(element, valueAccessor, allBindings){
// check if element has 'html' binding
if (!allBindings().html) return;
// get bound callback (don't care about context, it's ready-to-use ref to function)
var callback = valueAccessor();
// fire callback with new value of an observable bound via 'html' binding
callback(allBindings().html);
}
}