src/app/directives/file-select.directive.ts

Metadata

Relationships

Depends on

No results matching.

Index

Properties
Methods
Inputs
Outputs
HostListeners

Constructor

constructor(element: ElementRef)
Parameters :
Name Type Optional
element ElementRef No

Inputs

uploader
Type : FileUploader

Outputs

fileSelected
Type : EventEmitter<File[]>

HostListeners

change

Methods

isEmptyAfterSelection
isEmptyAfterSelection()

Checks if files exist in the input after input change

Returns : boolean
onChange
onChange()
Decorators :
@HostListener('change')
Returns : void

Properties

Protected element
Type : ElementRef
import {
  Directive,
  EventEmitter,
  ElementRef,
  Input,
  HostListener,
  Output
} from '@angular/core'
import { FileUploader } from '../models/FileUploader.class'

@Directive({
  selector: '[appFileSelect]',
  standalone: false
})
export class FileSelectDirective {
  @Input() uploader?: FileUploader
  @Output() fileSelected: EventEmitter<File[]> = new EventEmitter<File[]>()

  protected element: ElementRef

  constructor(element: ElementRef) {
    this.element = element
  }

  /**
   * Checks if files exist in the input after input change
   */
  isEmptyAfterSelection(): boolean {
    return !!this.element.nativeElement.attributes.multiple
  }

  @HostListener('change')
  onChange(): void {
    const files = this.element.nativeElement.files

    this.uploader?.addToQueue(files)

    this.fileSelected.emit(files)
    if (this.isEmptyAfterSelection()) {
      this.element.nativeElement.value = ''
    }
  }
}

results matching ""

    No results matching ""