I'm trying to create two separate components in Angular 2, they have no relation between each other so they are not nested. One Component is Users
and the other is Clients
.
What I do is this to call them separately is, use main.ts
as a main entry point, bootstrap both components there, and then call them in index.html
.
My questions are:
1- Can I not depend on a main entry point and call them directly from the index.html
like a directive in angular 1, or angular 2 you have to depend on a main entry component?
2- There a better way to bootstrap several controllers?
Here is the plunkr: http://plnkr.co/edit/fm70cmcWOSFrEKyRx4GF
My config.js
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
//map tells the System loader where to look for things
map: {
app: "./src"
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
}
}
});
My clients.ts
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
`,
directives: []
})
export class Clients {
constructor() {
this.name = 'Angular2'
}
}
My users.ts
import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
@Component({
selector: 'users',
providers: [],
template: `
<div>
<h2>Hello second component</h2>
</div>
`,
directives: []
})
export class Users {
constructor() {
this.name = 'Angular2'
}
}
My main.ts
//main entry point
import {bootstrap} from 'angular2/platform/browser';
import {Clients} from './clients';
import {Users} from './users';
bootstrap(Clients, [])
.catch(err => console.error(err));
bootstrap(Users, [])
.catch(err => console.error(err));
My index.html
<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-beta.8/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="config.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.8/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.8/angular2.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.8/http.dev.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
<users></users>
</body>
</html>