Angular 2 execute script after template render

后端 未结 4 1465
傲寒
傲寒 2021-01-31 13:32

So I have this slider component that use materializecss javascript. So after it\'s rendered, I need to execute

$(document).ready(function(){
    $(\'body\').on(\         


        
4条回答
  •  走了就别回头了
    2021-01-31 14:26

    I've used this method (reported here )

    export class AppComponent {
    
      constructor() {
        if(document.getElementById("testScript"))
          document.getElementById("testScript").remove();
        var testScript = document.createElement("script");
        testScript.setAttribute("id", "testScript");
        testScript.setAttribute("src", "assets/js/test.js");
        document.body.appendChild(testScript);
      }
    }
    

    it worked for me since I wanted to execute a javascript file AFTER THE COMPONENT RENDERED.

提交回复
热议问题