π Angular βletβ Directive
#angular #directive #ngTemplate
β Article link: https://javascript.plainenglish.io/angular-show-loading-spinner-for-observables-30f1bc96e19e
#angular #directive #ngTemplate
β Article link: https://javascript.plainenglish.io/angular-show-loading-spinner-for-observables-30f1bc96e19e
This media is not supported in your browser
VIEW IN TELEGRAM
π Resizable Columns in Angular Table
#angular #directive #ui_element
β Article link: https://levelup.gitconnected.com/how-to-create-resizable-columns-in-angular-scrollable-table-a-step-by-step-guide-5b8f766c1f60
π Code link: https://stackblitz.com/edit/angular-6x5egd?file=src%2Fapp%2Fdirectives%2Fcolumn-resize.directive.ts
#angular #directive #ui_element
β Article link: https://levelup.gitconnected.com/how-to-create-resizable-columns-in-angular-scrollable-table-a-step-by-step-guide-5b8f766c1f60
Please open Telegram to view this post
VIEW IN TELEGRAM
π1π₯1
π Angular Custom Directives: Custom Validation Directive
#angular #directive
#angular #directive
A custom validation directive in Angular allows you to create your own custom validation logic for form controls. In this example, weβll create a custom directive to validate that a password contains both letters and numbers. If the password doesnβt meet this criteria, weβll mark the form control as invalid and display an error message. Hereβs how you can create this custom validation directive
<input
type="password"
name="password"
[(ngModel)]="user.password"
#password="ngModel"
appPasswordValidator="[A-Za-z]+[0-9]+"
required
/>
<div *ngIf="
password.invalid
&& (password.dirty || password.touched)
">
<div *ngIf="password | hasError :'required'">
Password is required.
</div>
<div *ngIf="password | hasError :'passwordPattern'">
Password must contain letters and numbers.
</div>
</div>
π Angular Custom Directives: Autofocus Directive
#angular #directive
#angular #directive
In this example, we apply the appAutofocus directive to an input element. When the page loads, this input field will automatically receive focus, making it convenient for the user to start typing immediately.
<input
type="text"
placeholder="Auto-focused input"
appAutofocus
/>
import { Directive, ElementRef, AfterViewInit } from '@angular/core';
@Directive({
selector: '[appAutofocus]'
})
export class AutofocusDirective implements AfterViewInit {
constructor(private el: ElementRef) {}
ngAfterViewInit() {
this.el.nativeElement.focus();
}
}β€3
π Angular Custom Directives: Lazy Load Images Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular for lazy-loading images can help improve the performance and user experience of your website by loading images only when they are visible on the screen. In this example, weβll create a custom directive named appLazyLoad to implement lazy loading for images.
<img
src="placeholder.jpg"
data-src="lazy-image.jpg"
alt="Lazy-loaded image"
appLazyLoad
/>
π2β€1
π Angular Custom Directives: Drag-and-Drop Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular for drag-and-drop functionality can enhance the user experience by allowing users to interact with elements on your page. In this example, weβll create a custom directive named appDraggable to enable drag-and-drop functionality for elements.
<div appDraggable>
Drag me around!
</div>
β€3
π Angular Custom Directives: Ellipsis Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to add ellipsis to text that overflows its container can help improve the readability and aesthetics of your user interface. In this example, weβll create a custom directive named `appEllipsis` to add an ellipsis to text that exceeds its container's width.
<div appEllipsis>
This is a long text that will be truncated with an ellipsis if it overflows its container.
</div>
π Angular Custom Directives: Click Outside Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to handle clicking outside a specific element, like a dropdown or modal, can enhance the user experience by allowing you to close these elements when users interact with the rest of the page. In this example, weβll create a custom directive named appClickOutside to close a dropdown when users click outside of it.
<div
appClickOutside
(appClickOutside)="closeDropdown()"
>
<button (click)="toggleDropdown()">
Toggle Dropdown
</button>
<div
*ngIf="dropdownOpen"
class="dropdown"
>
Dropdown content
</div>
</div>
import { Directive, ElementRef, Output, EventEmitter, HostListener } from '@angular/core';
@Directive({
selector: '[clickOutside]'
})
export class ClickOutsideDirective {
@Output() clickOutside = new EventEmitter<void>();
constructor(private el: ElementRef) {}
@HostListener('document:click', ['$event'])
onClick(event: Event): void {
const element = this.el.nativeElement;
if (!element.contains(event.target)) {
this.appClickOutside.emit();
}
}
}π₯2π1
π Angular Custom Directives: Confirm Dialog Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to implement a confirm dialog can help you standardize and simplify the process of confirming actions before executing them, such as deleting items or making irreversible changes. In this example, weβll create a custom directive named appConfirmDialog to open a confirmation dialog before executing an action.
<button
[appConfirmDialog]="'Are you sure you want to perform this action?'"
>Delete</button>
π Angular Custom Directives: Infinite Scroll Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular for implementing infinite scrolling can significantly improve the user experience, especially when dealing with long lists of data. In this example, weβll create a custom directive named appInfiniteScroll to load additional content as the user scrolls down a page.
<div
appInfiniteScroll
class="scrollable-content"
(scrolled)="loadMoreData()"
>
<!-- Your list of items -->
</div>
import { Directive, ElementRef, HostListener, Input, Output, EventEmitter } from '@angular/core';
@Directive({
selector: '[appInfiniteScroll]'
})
export class InfiniteScrollDirective {
@Input() scrollThreshold = 100;
@Output() scrolled = new EventEmitter<void>();
constructor(private el: ElementRef) {}
@HostListener('scroll', ['$event'])
onScroll(event: Event): void {
const {
scrollHeight,
scrollTop,
clientHeight
} = event.target as HTMLElement;
const atBottom = scrollHeight - scrollTop <= clientHeight + this.scrollThreshold;
if (atBottom) {
this.scrolled.emit();
}
}
}π1
π Angular Custom Directives: Highlight Search Results Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to highlight search results within a block of text can improve the user experience when searching for specific terms in your application. In this example, weβll create a custom directive named `appHighlightSearch` to highlight search results in a text block.
<p [appHighlightSearch]="searchQuery">
Lorem ipsum dolor sit amet, ...
</p>
π2π1
π Angular Custom Directives: Responsive Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to control the visibility of elements based on the screen size can help you create responsive designs. In this example, weβll create a custom directive named appResponsive to show or hide elements based on the screen size.
<div [appResponsive]="'md, lg'">
This content is visible on medium and large screens.
</div>
π Angular Custom Directives: Input Mask Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to apply an input mask can help ensure that users enter data in a specific format. In this example, weβll create a custom directive named appInputMask to format and validate an input as a phone number.
<input
type="text"
[appInputMask]="'(999) 999-9999'"
>
π1
π Angular Custom Directives: Copy to Clipboard Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to allow users to copy content to the clipboard can enhance the usability of your application. In this example, weβll create a custom directive named appCopyToClipboard to enable users to copy text when they click on an element.
import { Directive, Input, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appCopyToClipboard]'
})
export class CopyToClipboardDirective {
@Input() appCopyToClipboard: string;
constructor(private el: ElementRef) {}
@HostListener('click')
onClick() {
if (this.appCopyToClipboard) {
const textarea = document.createElement('textarea');
textarea.value = this.appCopyToClipboard;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
}
}<button [appCopyToClipboard]="'Text to copy'">
Copy to Clipboard
</button>
π1
π Angular Custom Directives: Tooltip Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to display tooltips can enhance user interfaces by providing additional information when users interact with specific elements. In this example, weβll create a custom directive named appTooltip to show tooltips on hover.
<button
[appTooltip]="'Click me to learn more'"
>Learn More</button>
π Angular Custom Directives: Disable Right-Click Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to disable right-clicking can be useful in scenarios where you want to prevent users from accessing context menus or taking specific actions using the right mouse button. In this example, weβll create a custom directive named `appDisableRightClick` to prevent right-clicking on elements.
import { Directive, HostListener } from '@angular/core';
@Directive({
selector: '[appDisableRightClick]'
})
export class DisableRightClickDirective {
constructor() {}
@HostListener('contextmenu', ['$event'])
onRightClick(event: Event): void {
event.preventDefault();
}
}<div appDisableRightClick>
Right-clicking is disabled on this element.
</div>
π4
π Angular Custom Directives: TimeAgo Directive
#angular #directive
#angular #directive
Creating a custom directive in Angular to display a time ago (relative time) can be useful for showing how long ago an event occurred. In this example, weβll create a custom directive named appTimeAgo to display the time difference in a user-friendly format.
<p [appTimeAgo]="postDate"></p>
π1
π4
πAngular: Creating custom structural directives accepting multiple parameters
#angular #directive
β Article link
#angular #directive
As applications grow in size and as the conditions become more complex, instead of stuffing multiple conditions inside a *ngIf or *ngSwitch, we can opt for creating a more cleaner custom structural directive.
β Article link
π1π₯1
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
π₯4π2