Angular2 load component from external file with javascript

Fredrik Jacobson

I'm following the angular tutorial on the basics of Angular2, trying to translate it to javascript since it is currently only available for Typescript.

I currently have two files: app.component.js and hero-detail.component.js. Located in app.component.js i have my AppComponent. From this, I would like to load the component in hero-detail.component.js as a directive.

My current code looks like this, but I can't figure out how to load HeroDetailComponent:

app.AppComponent =
ng.core.Component({
  selector: 'my-app',
  inputs : ['hero'],
  directives: [HeroDetailComponent],
  template: `<h1>{{title}}</h1>My Heroes<h2></h2>
             <ul class="heroes">
             <li *ngFor="let hero of heroes" 
             [class.selected]="hero === selectedHero"
             (click)="onSelect(hero)">
             <span class="badge">{{hero.id}}</span> {{hero.name}}
             </li>
             </ul>
             <my-hero-detail [hero]="selectedHero"></my-hero-detail>
             `
})
.Class({
  constructor: function() {
  this.title = 'Tour of Heroes'
  this.heroes = HEROES
  this.selectedHero = null
  },
  onSelect(hero) { this.selectedHero = hero; }
});

})(window.app || (window.app = {}));enter code here
iGbanam

In Javascript, all variables are bound to the app which is in turn bound to the window.

You should define your HeroDetailComponent the same way AppComponent is defined.

(function (app) {
  app.HeroDetailComponent = ng.core.Component({
    selector: 'hero-detail',
    inputs: ['hero'], // <-- this is necessary to receive the "selectedHero"
    template: ` <!-- ... --> `
  }).Class({
    constructor: function () {
    }
  });
})(window.app || (window.app = {}));

Make sure to include the file in your index.html

<!-- 2. Load our 'modules' -->
<script src='app/hero.js'></script>
<script src='app/hero-detail.component.js'></script>
<script src='app/app.component.js'></script>
<script src='app/main.js'></script>

In your AppComponent, add the newly created HeroDetailComponent like so

app.AppComponent = ng.core.Component({
  directives: [app.HeroDetailComponent], <-- // note the dependence on the `app` object
  // ... and other things
}).Class({ /* Class Declaration */ });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript function in external HTML file not invoked when an object is passed as parameter from an Angular2 component

From Dev

Angular2 - Dynamically load component from module

From Dev

How to load an external file in JavaScript?

From Dev

Run external javascript file on load

From Dev

load json from external file

From Dev

Angular 2: import external js file into component

From Dev

angular2 child component does not load on subsequent event fired from parent component

From Dev

How to use javascript functions in an Angular 2 component from a different file

From Dev

Load an external javascript file before another?

From Dev

PHP in external javascript file causing it to not load

From Dev

Load external CSS into component

From Dev

Load an external JS file from a JS class

From Dev

Load file from external storage using chooser

From Dev

Load Subroutines From External Script File

From Dev

Load an external JS file from a JS class

From Dev

Load DIV tag from external file

From Dev

getElementById from an external javascript file

From Dev

getElementById from an external javascript file

From Dev

How to load external JS file in component using ember-cli

From Dev

Reactjs component in an external js file does not load sometime

From Java

Angular2 - Focusing a textbox on component load

From Dev

Can I load a shader into My JavaScript code from an external text file?

From Dev

How to automatically execute a javascript function from an external js file on page load with a specific condition

From Dev

How to automatically execute a javascript function from an external js file on page load with a specific condition

From Dev

How to load script file from component html?

From Dev

How to load script file from component html?

From Dev

JavaScript load a file not from cache

From Dev

JavaScript load a file not from cache

From Dev

Angular 5 : Load external CSS file - Not allowed to load local resource

Related Related

  1. 1

    JavaScript function in external HTML file not invoked when an object is passed as parameter from an Angular2 component

  2. 2

    Angular2 - Dynamically load component from module

  3. 3

    How to load an external file in JavaScript?

  4. 4

    Run external javascript file on load

  5. 5

    load json from external file

  6. 6

    Angular 2: import external js file into component

  7. 7

    angular2 child component does not load on subsequent event fired from parent component

  8. 8

    How to use javascript functions in an Angular 2 component from a different file

  9. 9

    Load an external javascript file before another?

  10. 10

    PHP in external javascript file causing it to not load

  11. 11

    Load external CSS into component

  12. 12

    Load an external JS file from a JS class

  13. 13

    Load file from external storage using chooser

  14. 14

    Load Subroutines From External Script File

  15. 15

    Load an external JS file from a JS class

  16. 16

    Load DIV tag from external file

  17. 17

    getElementById from an external javascript file

  18. 18

    getElementById from an external javascript file

  19. 19

    How to load external JS file in component using ember-cli

  20. 20

    Reactjs component in an external js file does not load sometime

  21. 21

    Angular2 - Focusing a textbox on component load

  22. 22

    Can I load a shader into My JavaScript code from an external text file?

  23. 23

    How to automatically execute a javascript function from an external js file on page load with a specific condition

  24. 24

    How to automatically execute a javascript function from an external js file on page load with a specific condition

  25. 25

    How to load script file from component html?

  26. 26

    How to load script file from component html?

  27. 27

    JavaScript load a file not from cache

  28. 28

    JavaScript load a file not from cache

  29. 29

    Angular 5 : Load external CSS file - Not allowed to load local resource

HotTag

Archive