Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare nested components in Angular2

In the Angular2 tutorial a detail component is introduced by adding it to the @NgModule.

Instead, I would like to add it by the outer component (AppComponent) somehow importing it, so that only the outer component refers to the inner component.

I can't figure out how to do it. Old examples refer to the directives property, but directives no longer exist in the type ComponentMetadtaType. So this does not work

import { HeroDetailComponent } from './hero-detail.component';

@Component({
    selector: 'my-app',
    [..]
    directives: [HeroDetailComponent]
})
like image 641
Klas Mellbourn Avatar asked Sep 18 '16 15:09

Klas Mellbourn


People also ask

Can we create nested component in Angular?

Angular allows us to have a different child, or nested component, which is the same component we normally use in an Angular application. The difference between them is that this child component contains the logic which can be used into the parent component as a single unit.


1 Answers

You have to add directives and components to declarations: [] of a module.
If you want to only one component to be able to use a component, create a module that consists only of these two components.

@NgModule({
  imports: [BrowserModule],
  declarations: [AppComponent, FooComponent, BarDirective],
  ...
})
like image 89
Günter Zöchbauer Avatar answered Sep 23 '22 19:09

Günter Zöchbauer