Angular 5 - Loading Script After View Is Loaded

Multi tool use
Multi tool use


Angular 5 - Loading Script After View Is Loaded



I have a legacy script that I need to include in my angular application.
The thing about this script is that it relates to a specific component, and it has to be loaded only after the view of the component is loaded.
As for today, I succeeded to include it on OnInit function but sometimes (not always for some reason) the CLI throws an error about it.




import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-player-page',
templateUrl: './player-page.component.html',
styleUrls: ['./player-page.component.scss']
})
export class PlayerPageComponent implements OnInit {
public itemId: string;

constructor() {}

ngOnInit() {
//We loading the player srcript on after view is loaded
require('assets/scripts/player/player.js');
}

}



The script assumes that elements in the UI exists and it searches them by the id.
When adding it on top of the page, it doesn't work.
What is the best way to achieve the desired behavior?





I had a similar problem with Google Maps. The solution I found was to create the <script> tag and inserting it in the head / body.
– Jorgeblom
Jul 3 at 8:12






What error does the cli throws? and on ngOnInit, the view is not rendered yet, you should use ngAfterViewInit. Probably wouldn't matter, because loading the js should take longer than the app to go from onInit to afterViewInit
– PierreDuc
Jul 3 at 8:27



ngOnInit


ngAfterViewInit


onInit


afterViewInit





ERROR in src/app/player-page/player-page.component.ts(14,5): error TS2304: Cannot find name 'require'. @PierreDuc thanks for the help. I changed it to ngAfterViewInit
– Tal Humy
Jul 3 at 8:49






@Jorgeblom - Angular ignore script tags in components HTML as far as I know
– Tal Humy
Jul 3 at 8:51





@TalHumy just add declare const require: any; on top of your component, you can also use the standard typescript dynamic import: import('assets/scripts/player/player.js');
– PierreDuc
Jul 3 at 8:57



declare const require: any;


import('assets/scripts/player/player.js');




1 Answer
1



There are multiple solutions to this issue.



declare the require const on top of your component


require


declare const require: any;

import { Component, OnInit } from '@angular/core';

@Component({})
...



use the dynamic import() function from typescript


import()


ngAfterViewInit() {
//We loading the player script on after view is loaded
import('assets/scripts/player/player.js');
}



change the library to only start running after you call a function from the component, this way you can add it to the scripts array of your angular.json


angular.json





Using import solved it - so simple I don't know why I used require. The third option is great but it will force me to change other projects. Thank you very much
– Tal Humy
Jul 3 at 9:31






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

GGSleikNL0y JDguYPYr1,Unv8TuiX7ec4mmOHgd CEApYRzLvoB xx6VpM
jvQAxVOkBK5,FRscjfB3u Y

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications