nifCody nifCody - 4 days ago 6
Javascript Question

<script> tag elements are not working on Angular 2 CLI created application

I have create an application using angular cli. My application is the angular 2 application. I would like to run a

<script>
and put an alert box.

I have try to load the
<script>
on hello.component.html but it does not work. And also I try put the
<script></script>
inside the index.html but it also fails.

How do i implement
<script></script>
on angular 2 applications.

<script>
$(document).ready(function() {
alert("Hello from JS");
});
</script>

Answer

<script> tags in Angular2 component templates are just silently stripped.

You can use

class MyComponent {
  ngAfterContentInit() {
    alert("...");
  }

or add JavaScript to your TypeScript by other means like require

Comments