0

I am new in agnular2. I have used html5 theme and theme having multiple javascript files. Now the issue is I am trying to load that files in my angular2 app but it's not loading properly.

What I have did is just link up that scripts to my index.html file and first time when I open the page the scripts are working good but when I route the pages in my app then it's not loading into the app on each router change need to refresh the page.

Please help..!!!

3
  • If you guys have not knowledge then at least please dont give down vote... This will helpful to others too. Commented Oct 3, 2016 at 5:35
  • Sorry, but i didn't give down vote. Commented Oct 3, 2016 at 5:36
  • Sorry I was not pointing you.. This is for down voters Commented Oct 3, 2016 at 6:14

2 Answers 2

0

it's because your JS file are loaded before the components are generated. Try something like this: In your js file use constructor fn. /**/ app.js

function callSomething(){
  console.log('something has been called.');
}

/* in component.ts*/

declare var callSomething: any; //declare this at top;

now you can call it inside any method: new callSomething();

Sign up to request clarification or add additional context in comments.

3 Comments

no I am using npm start and run build to run my application
Ideally it should work, if you have included your js file, it has been already loaded, & it should be available, the only reason would be it's not getting initialised in views. How do you call it in your views.Can you share your any view where it is not getting called?
I have just included into index.html file not in child templates
0

I think you need to initialise it on your view.I have done something like this:

In app.js(which i have included in my index.html)

function scrollToId(id){
  //scroll fn
}

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

declare var scrollToId: any;
@Component({})
export class UserDetailsComponent implements OnInit {
  constructor(){}
  ngOnInit() {
    new scrollToId('registration-form');
  }
}

& it's working fine for me.

3 Comments

I have to use themes javascript files inside my angular2 app. it's not any specific function
Only thing i want to point out is, you need to trigger that function on view also. As per me it's the same scenario when you have to bind event on dynamically generated elements, i am triggering it when component is rendered or getting rendered.
first time when I check to the network tab inside console then see that all the js files are logged into network tab but after route change it is not available

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.