I'm trying to learn Angular 2, after a lot of error messages i finally got the routes working and i can display the component i want. Now i want to show my homepage component, and i want this component to have a navbar component in it.
Home.ts:
import {Component, View} from 'angular2/core';
import {Navbar} from './navbar/navbar'
@Component({
selector: 'home'
})
@View({
templateUrl: '/app/home/home.html'
})
export class Home {
constructor:(public navbar: Navbar) {
}
}
home.html:
<p>NICE!</p>
<navbar></navbar>
navbar.ts:
import {Component, View} from 'angular2/core';
@Component({
selector: 'navbar'
})
@View({
template: `
<ul>
<li>This is the navbar</li>
</ul>
`
})
export class Navbar{
}
The homepage is displaying, but the tag just stays <navbar></navbar>
when i inspect it. What am i doing wrong?
If you want the navbar
component, you need to add it in the directives
attribute of the parent component, not in its constructor, like so:
@Component({
selector: 'home',
templateUrl: '/app/home/home.html',
directives: [ Navbar ]
})
export class Home {
constructor() {
}
}
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