Structural directive in Angular
manipulate the DOM by adding/removing a set of elements. Attribute directives
on the other hand decorate the element with more content/features. Structural
directives can be identified with the * mark in front of them. In this example
we will create a simple structural directive *ngShow which will show/hide the embedded content based on a
condition. Let us start by creating a new directive using the following ng
command.
Showing posts with label Custom Directives. Show all posts
Showing posts with label Custom Directives. Show all posts
Tuesday, November 27, 2018
Monday, November 26, 2018
Passing values to Custom Attribute Directive
In the Highlighter directive custom attribute directive example we saw how to create a
custom attribute directive and use it in a component. In the Highlighter directive example we just had to add the directive name in the HTML
element and that’s it the directive used ElementRef
to get a handle of the DOM element and set the background color of the element.
There are cases where we want to pass some values from the HTML element to the directive to perform more meaningful operations in the directive. To pass values to the Directive class we have to declare the parameters as @Input in the directive class as follows.
export class ExpValidatorDirective implements OnChanges {
@Input() expression: string
...
}
And when using the directive in the Component, we can pass values to the directive like properties as follows.
<span expValidator [expression]="value to be passed to the Directive"> </span>
In the following example we will create an Expression validator directive, this directive will take 2 parameters the first one is a regular expression and the second one is the value to evaluate.
Wednesday, November 21, 2018
Angular Custom Directives
Directives in
angular are special notations which allows us to extend the behavior of DOM
elements in the view template. Like components directives also have a selector and
used in the view template to extend the functionality of HTML tags. When the
Angular compiler finds a directive in the view template it executes the class
behind the directive and modifies the DOM accordingly.
Angular provides built-in directives like ngIf, ngFor etc, apart from these we can also define our own custom directives and use them in the view template.
Custom directives can be created using the ng generate directive command as follows.
Angular provides built-in directives like ngIf, ngFor etc, apart from these we can also define our own custom directives and use them in the view template.
Custom directives can be created using the ng generate directive command as follows.
Subscribe to:
Posts (Atom)

