David Ericsson
David Ericsson

Reputation: 2660

Component not rendering Angular 2 Typescript

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?

Upvotes: 6

Views: 4138

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202138

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() {

  }
}

Upvotes: 11

Related Questions