2

I have some scripts in my html code in my angular 2 app and they do not load. When i check the console of the browser I do not see an attempt to even load the script? How can i load JS scripts in angular 2

div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
                    <div class="alert alert-dismissible fade in" role="alert">
                        <script type="text/javascript">
                              setInterval(function() {
                                $('#element').load('/Resources/speedtest.txt');
                            }, 1000);
                        </script>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span></button>
                        <span id="element">Loading...</span>
                    </div>
                </div>

There is an example of one of the scripts.. This is being used in a component called home and i use the template home.html where this is located.

here is the home component code.

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

@Component({
    selector: 'home',
    templateUrl: './home.html'
})
export class homecomponent { }

1 Answer 1

2

Angular just strips <script> tags from templates. You need other ways to load the script. For example using requirejs in your components code.

See also https://github.com/angular/angular/pull/12172

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

3 Comments

I had a feeling that was what was needed, so i basically need to put the js script in the export area of the component? Thank you very much as well!
This might help stackoverflow.com/questions/38103920/… I haven't used it myself yet.
ahhhh ok thank you this information helps me alot i can find the answer from here! Your help is greatly appreciated thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.