Creating Reusable Components in Angular
Reusable button component

Creating Reusable Components in Angular


Lets start with a basic button

Here in the image we could see 3 different buttons with different names, different styling and with their names, it indicates they perform 3 different actions.

No alt text provided for this image

Instead of using button tag multiple times, one component can be created and that can be used for all the three by passing values dynamically which is nothing but Reusability.

Step 1 => Create a component and add a button tag

No alt text provided for this image

Step 2: Accessing the created component , which is void-button in this case, with its selector i.e.., app-void-button as shown in the image below

No alt text provided for this image

If inspected, here we can see the rendered component

No alt text provided for this image

Step 3: Sending text of a button dynamically. For that @Input decorator is used. This helps to pass data from parent component to child component.

No alt text provided for this image

Step 4: Passing text from parent component to child component using property binding

No alt text provided for this image

Step 5: Now, In order to render that text into the button, string interpolation is used as shown in the image below which is {{ btnName}}

No alt text provided for this image

Let's understand it better and notice the difference by passing different names for the same component


For the same component three different names has been passed.

No alt text provided for this image

Step 6: Adding styling dynamically in the similar way by which name has been binded using @input decorator. Passing as btnClass property from child component

No alt text provided for this image

Property binding for class with btnClass property in html

No alt text provided for this image

Step 7: Passing the class name in the parent component

No alt text provided for this image

Step 8: After rendering, if inspected, same component is reused three times from the picture below with different text and distinct class names

No alt text provided for this image

In this way, we can use the same component multiple times for all the most commonly used widgets like dropdowns, accordions, tables and many more by passing data dynamically.

In this post, Sougandi Kalidoss shares her daily learning experience with Angular. She seems to have learned about creating a button using Angular. It's wonderful to see developers actively engaging in continuous learning and sharing their progress with the community. Keep up the great work, Sougandi!?For more information visit https://www.dhirubhai.net/feed/update/urn:li:activity:7091750990537728000

回复
Neha Pathak

CSM# Certified #Scrum Master at PeopleTech

1 年

Great Job dear

要查看或添加评论,请登录

Sougandika L的更多文章

  • Lifecycle Methods in Functional components

    Lifecycle Methods in Functional components

    Lifecycle refers to the series of phases that a React component goes through from its creation, rendering and eventual…

  • Are you excited to know about Event Flow in Javascript ??

    Are you excited to know about Event Flow in Javascript ??

    Order in which event triggered on DOM nodes is called event flow. There are 2 types of events: 1.

  • Angular 18 feature - Zoneless Change Detection

    Angular 18 feature - Zoneless Change Detection

    One of the most significant changes is the introduction of zoneless change detection. Reason : To avoid unnecessary…

  • Difference between Subject and Observable

    Difference between Subject and Observable

    1.Subject can be provider as well as consumer.

  • Why do we need package.json

    Why do we need package.json

    Most of us are concerned or focused on the place where we write our code, whether it might be styling (css) or…

  • SIGNAL in Angular

    SIGNAL in Angular

    With Change detection, incase of any change in one of the components, it check for all the components and then render…

    1 条评论
  • Reactive forms in Angular

    Reactive forms in Angular

    Angular provides 2 ways to work with forms – 1. Template driven forms 2.

  • Angular -Download Data in Excel format

    Angular -Download Data in Excel format

    Hello everyone, One of the most common usecases now a days is printing the data. In this article we will be…

  • Why Array ? Its declaration in Javascript

    Why Array ? Its declaration in Javascript

    Array is a special variable which holds more than one value at a time. It stores the homogenous elements.

  • Scope: What does it exactly mean?

    Scope: What does it exactly mean?

    Hello everyone, This is Sougandika. Do you want to know what is scope? Be with me till the end and I am sure that this…

社区洞察

其他会员也浏览了