2

I have an angular 2 app which has a template in the component as follows

app.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    templateUrl: 'templates/homepage.html',
    directives: []  
})
export class AppComponent {

}

I need to include external jquery files in the template homepage.html

I tried including the external script files in the homepage.html as follows doesn't seems to be working

<script src="/src/js/jquery.min.js"></script>
<script src="/src/js/jquery.scrollex.min.js"></script>
<script src="/src/js/jquery.scrolly.min.js"></script>
<script src="/src/js/skel.min.js"></script>
<script src="/src/js/util.js"></script>    
<script src="/src/js/main.js"></script>

boot.ts file looks like this

/// <reference path="../typings/browser.d.ts" />
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from "./app.component";

bootstrap(AppComponent);

index.html

<html>
<head>
    <base href="/">
    <title>Angular 2 Boilerplate</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
    <script src="node_modules/angular2/bundles/router.dev.js"></script>
    <script src="node_modules/angular2/bundles/http.js"></script>

<!-- 
    <link rel="stylesheet" href="src/css/app.css"> -->
</head>
<body>
<my-app>Loading...</my-app>

<script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        }
    });
    System.import('app/boot')
            .then(null, console.error.bind(console));
</script>
</body>
</html>    

I tried loading the .js files in the index.html itself but that is not working. I am fairly new to angular 2.

user2180794
  • 1,425
  • 7
  • 27
  • 50

1 Answers1

0

Script tags in components template are just removed.

A workaround is to add thw script tag imperatively or use require(...) to load it.

See also
- angular2: including thirdparty js scripts in component
- https://github.com/angular/angular/issues/7720
- How to Append <script></script> in javascript?

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567