0

I'm using Angular 5 and I want to include my script files .js

I'm using the script tag in the index.html

When I refresh the page everything works fine, but when i'm using SPA routing my js files does not included

Here's an exemple :

If I refreshed the page

enter image description here

When i'm using SPA navigation

enter image description here

Here's index.html where i include all my script whith script tag

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <base href="/">
    <title>Listeo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="../assets/css/colors/main.css" id="colors">
</head>
<body>
    <app-base></app-base>
    <script type="text/javascript" src="../assets/scripts/jquery-2.2.0.min.js"></script> 
    <script type="text/javascript" src="../assets/scripts/mmenu.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/chosen.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/slick.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/rangeslider.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/magnific-popup.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/waypoints.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/counterup.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/jquery-ui.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/tooltips.min.js"></script>
    <script type="text/javascript" src="../assets/scripts/custom.js"></script>
    <script type="text/javascript" src="../assets/scripts/switcher.js"></script>
</body>
</html>

and here's the navigation bar :

<nav id="navigation" class="style-1">
    <ul id="responsive">
        <li>
            <a [routerLink]="['home']" [routerLinkActive]="['current']">Home</a>
        </li>
        <li>
            <a [routerLink]="['restaurants']" [routerLinkActive]="['current']">Restaurants</a>
        </li>
    </ul>
</nav>

When I'm using the href everything wirks fine, but with [routerLink] I'm facing the problem.

6
  • Without being providing the relevant snippets of code it's impossible to tell you what the problem might be. Please rework your post. Commented Mar 25, 2018 at 15:35
  • @DiabolicWords done. Commented Mar 25, 2018 at 15:41
  • you need to trigger manually the script which turns the plain slider into the fancy one Commented Mar 25, 2018 at 15:52
  • @David is there any sources i can use ? thanks Commented Mar 25, 2018 at 15:56
  • well I don't know. Which of your scripts is changing the slider? Commented Mar 25, 2018 at 15:57

2 Answers 2

1

Are you using the Angular CLI? Then you can put your custom js files into .angular-cli.json

"scripts": [
    "<path_to_script",
    "<path_to_second_script"
]

Then you don't have to reference them in the index.html.

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

5 Comments

What exactly does not work when you reference them this way?
if i refreshed the page, the scripts files works, but when i used the spa navigation, the scripts dosen't load !
OK. Why should they load again? When navigating in a single page nothing gets loaded again, but the data you are loading manually from a server. What should the scripts do when you navigate?
i edit my question with an exemple what should a script file do.
OK. This seems to happen when the script is fully loaded automatically. So within the script a function gets called, when the script is loaded. But this is not possible when SPA navigation is used. Then you have to trigger script contents yourself.
0

You need to manually trigger the script

From looking at the documentation ( http://rangeslider.js.org/), the way to trigger the script is

 $('input[type="range"]').rangeslider();

So what you probably need to do is trigger this in your component's ngAfterViewInit method (the one using rangeslider). This need to be in ngAfterViewInit to make sure that the dom elements have been created.

//component.ts
ngAfterViewInit()
{
   $('input[type="range"]').rangeslider();
}

Comments

Your Answer

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