6

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?

1 Answer 1

11

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

  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This is indeed the answer for this question, but i'm afraid this throws me into the next error :(. Now my router is giving troubles again.
It seems my navbar didn't compile, compiled it again and works like a charm now! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.