Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 339
David Ericsson Avatar asked Feb 22 '16 19:02

David Ericsson


1 Answers

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

  }
}
like image 70
Thierry Templier Avatar answered Oct 27 '22 07:10

Thierry Templier