sizz sizz - 16 days ago 5
HTML Question

Angular 2 app doesnt load JS scripts in html

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 { }

Answer

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

Comments