I have two nested @Components
en angular 2. The view renders just fine but it always throws a javascript error the first time.
Here is my code in Typescript.
App HTML:
<body>
<my-app>loading...</my-app>
</body>
App Component:
import {bootstrap, Component} from 'angular2/angular2';
import {CanvasComponent} from "./CanvasComponent";
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>My Games</h2>
<div>
<my-canvas></my-canvas>
</div>
`,
directives: [CanvasComponent]
})
class AppComponent {
}
bootstrap(AppComponent);
Canvas Component:
import {bootstrap, Component, View} from 'angular2/angular2';
@Component({
selector: 'my-canvas'
})
@View({
template: `
<div>
<span>Balls:</span>
<div>{{canvas.length}}</div>
</div>
`
})
export class CanvasComponent {
canvas = [1,2,3];
}
bootstrap(CanvasComponent);
The error is:
EXCEPTION: The selector "my-canvas" did not match any elements
Remove bootstrap(CanvasComponent)
from your CanvasComponent
file. It's trying to bootstrap application second time using CanvasComponent
as a root and looking for my-canvas
element in your App HTML. Of course it can't find it.
I fixed the issue by changing the name in the index.html, you should be sure that the tag in the index.html are the same selector that in the main component.
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stylesheets/styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app> <!-- THIS TAG SHOULD BE THE SAME THAT THE SELECTOR IN THE MAIN COMPONENT -->
</body>
</html>
<!-- end snippet -->
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With