Pass data to custom directive from component/HTML in Angular2+ application

  idkblogs.com      August 9, 2017

Shubham Verma

Shubham Verma


Pass data to custom directive from component/HTML in Angular2+ application

In this artticle, we will learn how we can pass data in custom directory in angular 2+ app, In this we'll create a directory and pass two data from component or HTML to custom directory and later we will see the use of passed data in the custom directory code.

Before reading this article, I will recomend you to read this article and get the idea about how we can create a custom directory?

Let's start

Create an angular2+ app:

First we need to create an app using "ng new" command, let's create an app using below command:

ng new ang-directive
cd ang-directive
npm install

After successful "npm install" your angular demo app is created, Now open your terminam/CMD and locate your project root and follow the below steps:

Passing the data to custom directive from component/HTML in Angular2+ application, we need to create a custom directory, to do this go to your app and then go to src/app folder and create a file "test.directive.ts" and write the below code:

test.directive.ts: (src/app/test.directive.ts)


                      import { Directive, ElementRef, Input, OnInit } from ‘@angular/core’; 
                      @Directive({
                        selector: ‘[input-box]’
                      }); 
                        
                      export class TestDirectives implements OnInit {
                          @Input() nameData: string;
                          @Input() valueDate: string;
                          constructor(private elementRef: ElementRef) {
                        }
                        ngOnInit() {
                          console.log(“input-box keys : “, this.nameData, this.valueDate);
                        }
                      }
                      

Now, your directive has been created and you need to add this directive into your `app.module.ts`as:

app.module.ts:


                        import { NgModule } from ‘@angular/core’;
                        import { AppComponent } from ‘./app.component’;
                        import { TestDirectives } from ‘../directives/test.directive’;
                        
                        @NgModule({
                        declarations: [
                        AppComponent,
                        TestDirectives
                        ],
                        imports: [],
                        providers: [],
                        bootstrap: [AppComponent]
                        })
                        export class AppModule { }
                      

You will have to use your directive in your html and send data to the directive like in below code.

I am sending `nameData` and `valueDate` to the `test.directive.ts`:

or

Now, you are done, go to the console and see the output of line "console.log(“input-box keys : “, this.nameData, this.valueDate);". Now you can use data in the directive accordingly.


Conclusion:

In this article, we learned how we can create a directory in angular 2+, and how we can pass data from component/HTML to the directory.


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.



OTHER POSTS

MONTHLY POSTS