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.
Support our IDKBlogs team
Creating quality content takes time and resources, and we are committed to providing value to our
readers.
If you find my articles helpful or informative, please consider supporting us financially.
Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce
high-quality content.
Thank you for your support!
Thank you
I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go.
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.