How to load external scripts dynamically in Angular?

前端 未结 15 749
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:48

I have this module which componentize the external library together with additional logic without adding the

15条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 07:22

    @d123546 I faced the same issue and got it working now using ngAfterContentInit (Lifecycle Hook) in the component like this :

    import { Component, OnInit, AfterContentInit } from '@angular/core';
    import { Router } from '@angular/router';
    import { ScriptService } from '../../script.service';
    
    @Component({
        selector: 'app-players-list',
        templateUrl: './players-list.component.html',
        styleUrls: ['./players-list.component.css'],
        providers: [ ScriptService ]
    })
    export class PlayersListComponent implements OnInit, AfterContentInit {
    
    constructor(private router: Router, private script: ScriptService) {
    }
    
    ngOnInit() {
    }
    
    ngAfterContentInit() {
        this.script.load('filepicker', 'rangeSlider').then(data => {
        console.log('script loaded ', data);
        }).catch(error => console.log(error));
    }
    

提交回复
热议问题