Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js 如何从ko.bindingHandler中访问jquery.template内容?_Knockout.js_Jquery Templates - Fatal编程技术网

Knockout.js 如何从ko.bindingHandler中访问jquery.template内容?

Knockout.js 如何从ko.bindingHandler中访问jquery.template内容?,knockout.js,jquery-templates,Knockout.js,Jquery Templates,有一个具有绑定的元素(让它成为“a”)。此元素的内容是使用knockout.js/jquery.tmpl模板生成的。是否可以从元素“A”上调用的绑定处理程序的内部获取此内容 下面是一个简单的例子: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

有一个具有绑定的元素(让它成为“a”)。此元素的内容是使用knockout.js/jquery.tmpl模板生成的。是否可以从元素“A”上调用的绑定处理程序的内部获取此内容

下面是一个简单的例子:

<html>
    <head>      
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
        <script src="http://cloud.github.com/downloads/SteveSanderson/knockout/jquery.tmpl.js"></script>
        <script src="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-1.3.0beta.debug.js"></script>
    </head>
    <body>
        <!-- Some simple tmpls -->
        <script id="wrapperTmpl" type="text/html">
            <div data-bind="showBug:true">                
                <div data-bind="template: 'buggyTmpl'"></div>               
            </div>
        </script>

        <script id="buggyTmpl" type="text/html">
            <div class="buggy">I am out of reach.</div>
        </script>

        <!--
            Knockout won't handle{{tmpl}} calls which are not wrapped in knockout constructions.
        -->
        <div data-bind="template: 'wrapperTmpl'"></div>

        <script>
            <!-- Refers to elements from the child rendered using tmpl -->
            ko.bindingHandlers.showBug = {
                init: function (elem, valueAccessor, allBindingAccessors, model) {
                    var $buggyElem = $(".buggy", elem);
                    console.log("$(elem).html(): ", $(elem).html());        
                    console.log("$('.buggy'): ", $buggyElem);

                }
            };


            ko.applyBindings({});       
        </script>
    </body>
</html>

我够不着。
ko.bindingHandlers.showBug={
init:函数(元素、valueAccessor、AllBindingAccessor、模型){
变量$buggyelm=$(“.buggy”,elem);
log(“$(elem.html():”,$(elem.html());
log(“$('.buggy'):”,$buggyelm);
}
};
ko.应用绑定({});

如果有任何帮助,我将不胜感激。

唯一的问题是,在执行自定义绑定后,模板将被呈现。您可以通过在setTimeout中执行init代码来处理此问题,如:

ko.bindingHandlers.showBug = {
    init: function (elem, valueAccessor, allBindingAccessors, model) {
        setTimeout(function() {
            var $buggyElem = $(".buggy", elem);
            console.log("$(elem).html(): ", $(elem).html());        
            console.log("$('.buggy'): ", $buggyElem);           
        }, 0);
    }
};

谢谢你,@RPNiemeyer。超时是一种可能的解决方法,但是是否有一些处理类似示例的常规做法?除了使用
afterRender
回调之外,实际上没有其他模式可以处理这种情况。模板需要先渲染,然后才能对元素进行操作。