2

Hello first of all I must say I am sorry but I don't know how to express the question better, is the reason I am not being able to find an answer by myself.

What I'm talking about is how to load a component inside another one, I need to indicate it in the directive. Here is a very small example that I did from scratch because I am not able to find the right syntax:

http://plnkr.co/edit/gFsqGJmmayOsewL3EfLf

import {Component} from 'angular2/core'
import {Prueba} from './prueba'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <prueba></prueba>      
    </div>
  `,
  directives: [Prueba]
})
export class App {
  constructor() {
    this.name = 'Angular2'
  }
}

So as you can see in app.ts there is a directives inside component, if I remove that it does not work. I am not 100% sure why, but it's how I learned.

So next step, I wanted to have several components, so I can have Prueba and another that adds something extra (for starters, another "phrase", but the idea is to add something similar to THIS: http://plnkr.co/edit/SVPNwk?p=preview ). However I find myself unable to find the right syntax, anything I try makes even this simple example to fail.

As I said, I do not understand what am I missing, I have a new component, I import it, I use the selector, and so on, but it simply explodes. What concepts am I missing?

If I am still not explaining myself properly enough, this is the theoric concept I am talking about:

angular.io/docs/ts/latest/cheatsheet.html (I cannot post more than two links... anyway its the @Component part, that's the documentation I'm checking out).

monkey intern
  • 705
  • 3
  • 14
  • 34
  • It's not clear what the problem is. What do you exactly mean by "if I remove that it does not work.". If you remove "" or "directives: [Prueba]"? – Thierry Templier Mar 31 '16 at 10:51
  • what you realy want to do ? and whats the error you got ? – Pardeep Jain Mar 31 '16 at 10:53
  • I am sorry you are correct it isn't clear @ThierryTemplier . I meant if I remove Prueba from "directives: [Prueba]", it ends up not showing anything (which as far as I know, it is expected). – monkey intern Mar 31 '16 at 11:19
  • What I really want to do @PardeepJain is to be able to add that calendar I showed, because I know it DOES work, to an existing project. I am finding myself unable to do it, and as far as I got, is because of directives (when, it really shouldn't be the problem, since what little documentation I've found points to directives being an array, so it should accept multiple directives). – monkey intern Mar 31 '16 at 11:20
  • 1
    This is your starting point https://angular.io/docs/ts/latest/ – Eric Martinez Mar 31 '16 at 11:21
  • @monkeyintern Yes that's the normal behavior when you remove from the `directives` attribute. What is this calendar component? Did you try to add its class within this attribute and use it in your `App` component using the calendar component selector? – Thierry Templier Mar 31 '16 at 11:27

1 Answers1

1

In Angular2 there is a difference between a component and a directive:

  • A component gathers a view (template) with some properties and processing (the component class)
  • There are two kinds of directives:
    • Attribute directive. It changes the appearance or behavior of a DOM element
    • Structural directive. It changes the DOM layout by adding and removing DOM elements.

A component can be used in another component using its selector. You need to explicitly define it in the directives attribute of the container component. Whereas the attribute is called directives, you can put in it both components and directives. You can also provide parameters to a component and react on its events.

Here is a sample:

  • Sub component

    @Component({
      selector: 'sub',
      template: `
        <div>Sub</div>
      `
    })
    export class SubComponent {
    }
    
  • Container component:

    @Component({
      selector: 'comp',
      template: `
        <div>
          <sub></sub>
        </div>
      `,
      directives: [ SubComponent, AnotherComponent ]
    })
    export class ContainerComponent {
    }
    

A directive will apply on an existing element also based on it selector.

Here is a sample:

  • Sub component

    @Directive({
      selector: '[dir]'
    })
    export class DirDirective {
      constructor(el: ElementRef) {
        // el.nativeElement corresponds to the DOM element
        // the directive applies on
        el.nativeElement.style.backgroundColor = 'yellow';
      }
    }
    
  • Container component:

    @Component({
      selector: 'comp',
      template: `
        <div dir>Some text</div>
      `,
      directives: [ DirDirective ]
    })
    export class ContainerComponent {
    }
    

The directives attribute

To tell a bit more about the directives attribute. If the component / directive isn't a platform one, you need to explicitly define into this directive. If not, the component / directive won't apply.

This attribute can accept several values since it's an array:

@Component({
  selector: 'comp',
  template: `
    <div>
      <sub></sub>
      <another></another>
    </div>
  `,
  directives: [ SubComponent, AnotherComponent ]
})
export class ContainerComponent {
}
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • I see. I knew some of this (because of the documentation) but not all, I think I have a way better grasp of it so thank you! Problem must be somewhere else if it IS possible to add several directives on a given Component. – monkey intern Mar 31 '16 at 11:30
  • You're welcome! Yes you can have / use several components / directives into a component... – Thierry Templier Mar 31 '16 at 12:01
  • 2
    The `directives` attribute doesn't seem to be there in Angular 4 anymore. – hirse Apr 27 '17 at 09:29