Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Component How to Import Directive Component?

I just upgraded to angular2 final release and there were alot of breaking changes from rc4. One of which is there is no longer a Directives collection on the component... I uesd to do something like this:

@Component({
    selector: 'resource-tab',
    templateUrl: './app/applicationMgmt/resource/resource-tab.html',
    directives: [
        NgSwitch,
        NgSwitchCase,
        NgSwitchDefault,
        OtherResourceEditorComponent,
        MvcResourceEditorComponent,
        WcfResourceEditorComponent,
        WebResourceEditorComponent,
        WebApiResourceEditorComponent,
        ConfigResourceEditorComponent
    ]    
})

to reference other components that are used as directives on the view template... How does one do this now?

like image 856
cobolstinks Avatar asked Sep 22 '16 18:09

cobolstinks


People also ask

How do I add a directive to a component?

To create a custom directive we have to replace @Component decorator with @Directive decorator. So, let's get started with creating our first Custom Attribute directive. In this directive, we are going to highlight the selected DOM element by setting an element's background color. Create an app-highlight.

Can we use component as directive in Angular?

by putting brackets around the selector, that informs Angular of how it can be used. in this case [nbMenuItem] means this can be use as an attribute of a DOM element, and as such as an directive. Nailed the answer!

Can we use component as directive?

Using @Component, we can create our HTML template which can be injected into the DOM at run time. To wrap it up, use @Directive to create a custom directive that can be used to modify the element or structure of the DOM. And use @Component, if you want to create the reusable UI components with custom behavior.


3 Answers

The best way to do it would be to create a ResourceEditorModule like this:

@NgModule({
    declarations:[
            OtherResourceEditorComponent,
            MvcResourceEditorComponent,
            WcfResourceEditorComponent,
            WebResourceEditorComponent,
            WebApiResourceEditorComponent,
            ConfigResourceEditorComponent
    ],
    exports:[
            OtherResourceEditorComponent,
            MvcResourceEditorComponent,
            WcfResourceEditorComponent,
            WebResourceEditorComponent,
            WebApiResourceEditorComponent,
            ConfigResourceEditorComponent
    ]
})
export class ResourceEditorModule{}

and add ResourceEditorModule as import in your AppModule:

@NgModule({
    imports:[
        ...
        ...
        ResourceEditorModule
    ],
    ...
    ...
    ...
})
export class AppModule{}

The idea bedhind modules is to split your components, directives, pipes and services in groups that belongs to the same "package", like everything you need to use http for HttpModule, everything you need to use forms for FormsModule

like image 124
Supamiu Avatar answered Oct 13 '22 02:10

Supamiu


You should specify Components, directives and Pipes under declarations property of the NgModule. So basically the easiest way for me upgrading my angular 2 project to 2.0.0. version was defining one module for the app (app.module.ts) put all there and then start breaking it smaller modules. Good reference for this can be found here

like image 35
galvan Avatar answered Oct 13 '22 01:10

galvan


In Angular2.0.0 in-built directives are accessible/available with BrowserModule,. You just need to import it as shown below and you'll be able to most of in-built directives.

For customDirective, you need to declared them with declarations metadata as shown,

    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';

   /*---------import all custom directives-----------*/

   import { OtherResourceEditorComponent} from 'valid path';
   import { MvcResourceEditorComponent } from 'valid path';
   ...
   ...   

    @NgModule({
      imports:      [ BrowserModule],
      declarations: [ AppComponent,OtherResourceEditorComponent,MvcResourceEditorComponent,... ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }
like image 23
Nikhil Shah Avatar answered Oct 13 '22 03:10

Nikhil Shah