Durundal Google Translate

后端 未结 2 609
遥遥无期
遥遥无期 2021-01-28 17:37

How can I bind Google translate in Durundal Shell.js , shell.html?

html

script<

相关标签:
2条回答
  • 2021-01-28 18:23

    Durandal doesn't render script tags within views. To render them, you should use knockout custom bindings:

    ko.bindingHandlers.googleTranslate = {
      update: function( element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
        var googleElement = valueAccessor();
        $(element).html('<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script> <script>function googleTranslateElementInit() { new google.translate.TranslateElement({ pageLanguage: "en", autoDisplay: false }, " + 'googleElement' + ");}</script>');
      }
    };
    

    Use it in your shell.html:

    <div id="google_translate_element"></div>
    <div data-bind="googleTranslate:'google_translate_element'"></div>
    
    0 讨论(0)
  • 2021-01-28 18:28

    See Durandal's Composition functionality: http://durandaljs.com/documentation/Using-Composition/

    Example:

    <div>
        <div data-bind="compose:'views/google_translate.html'"></div>
    </div>
    
    0 讨论(0)
提交回复
热议问题