Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
up vote 5 down vote accepted

If you want the navbar component, you need to add it in the directives attribute of the parent component, not in its constructor:

Component({
  selector: 'home',
  templateUrl: '/app/home/home.html',
  directives: [ Navbar ]
})
export class Home {
  constructor:() {
  }
}
share|improve this answer
    
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. – David Ericsson Feb 22 at 19:38
    
You're welcome! Really? What is the error? – Thierry Templier Feb 22 at 19:40
1  
It seems my navbar didn't compile, compiled it again and works like a charm now! Thanks! – David Ericsson Feb 22 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.