PatrickJS / PatrickJS-starter

MFE Starter
MIT License
10.32k stars 4.9k forks source link

Getting JavaScript to work in Angular 2-webpack #1612

Closed kenadet closed 7 years ago

kenadet commented 7 years ago

How can I get javascript or jquery in my html or app component to work, noticed they are not working. The javascript in this page were not detected.

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

@Component({
    selector: 'header',
    styleUrls: [ 'app.scss'],
    templateUrl: 'topnav.component.html',
})

export class NavComponent {

constructor(){}     

    gettheme(color: string): void {
        var link: any = $('<link>');
        link
            .appendTo('head')
            .attr({type : 'text/css', rel : 'stylesheet'})
            .attr('href', 'themes/app-'+color+'.css');
    }

}
pradeepgill007 commented 7 years ago

Hi @kenadet

This can be done by pure Javascript, Please find below code may help you.

`
export class NavComponent {

constructor(){}

ngOnInit(){ let css_link = 'http://abc.com/css/abc.css' this.getDynamicThemingCSS(css_link); } getDynamicThemingCSS(cssLink){ let linkTag = document.createElement('link'); linkTag.type = 'text/css'; linkTag.rel = 'stylesheet'; linkTag.href = cssLink; document.head.appendChild(linkTag); }

} `

kenadet commented 7 years ago

This is great