Directive name and @Input() property name are same with no alias.
Now, we can bind attribute directive as given below.
- <p [textSize]="txtsize"> textSize Directive Demo using Bracket []</p>
Scenario 3
Directive name and @Input() alias are same no matter what the @Input() property name is.
- @Input('textSize') tsize: string;
Again, we can use the attribute directive as given below.
- <p [textSize]="txtsize"> textSize Directive Demo using Bracket []</p>
Output
Custom attribute directives - User input color name in HTML template.
Code of color-input.directive.ts
- import { Directive, ElementRef, Input, AfterViewInit } from '@angular/core';
-
- @Directive({
- selector: '[satyaColor]'
- })
- export class ColorInputDirective implements AfterViewInit{
- @Input() inputColor: string;
- constructor(private elRef: ElementRef) {
- }
- ngAfterViewInit(): void {
- this.elRef.nativeElement.style.color = this.inputColor;
- }
- }
Code Description
To take the user input in a custom directive, I have created a property decorated with @Input(). We will create a myColor directive that will take user input color name in HTML template.
AfterViewInit - It is the lifecycle hook that is called after a component view has been fully initialized. To use AfterViewInit, our class will implement it and override its method ngAfterViewInit().
Code of app.component.html
- <div satyaColor inputColor="red">color input directive by Satya</div>
Code Description
Now in the HTML template, we can use satyaColor directive and it has taken input color name that is red in HTML Template.
OUTPUT
Custom attribute directives - Change text color, background color and font size
Code Ref of theme.directive.ts
- import { Directive, ElementRef } from '@angular/core';
-
- @Directive({
- selector: '[satyaTheme]'
- })
- export class satyaThemeDirective {
- constructor(elRef: ElementRef) {
- elRef.nativeElement.style.color = 'blue';
- elRef.nativeElement.style.backgroundColor = 'yellow';
- elRef.nativeElement.style.fontSize = '70px';
- }
- }
Code Description
I have created a custom attribute directive that will change text color, background color and font size. To change the HTML attribute we need to use ElementRef that has the property nativeElement and using it we can change HTML attribute of an element in the DOM.
Code Ref. Of app.component.html
- <div satyaTheme> Custom Attribute Theme Directive By Satyaprakash </div>
Code Description
Before using the satyaTheme attribute directive, we need to configure the satyaThemeDirective class in the application module in the declarations block of @NgModule. Now, we are ready to use satyaTheme directive.
OUTPUT
Fetch source code from GitHub,
Note
Here, I have uploaded my source code by excluding the node_modules folder due to it being a big file and it can be downloaded by using NPM. NPM is the node package manager, which installs packages locally into a project, specifically into the node_modules folder.
SUMMARY
- User input text size and its different scenarios.
- User input color name in HTML template.
- Change HTML attribute of an element in the DOM.