Angular 4 script not working after changing route

后端 未结 6 1050
萌比男神i
萌比男神i 2020-12-11 09:59

while changing the routes to homepage, javascript and jquery code is not loading in Angular 5

6条回答
  •  长情又很酷
    2020-12-11 10:12

    you need to load your script in ngOnInit(). This is because when angular changes its route then it does not fire document.ready. That's why JS does not called. Adding in ngOnInitwill solve the problem. It loads JS when ever your ngOnInit is being called.

    ngOnInit(){
        $.getScript('src/assets_v2/js/main.js');
        // do rest of your stuff here.
    }
    

提交回复
热议问题