Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function outside of angular directive component

Is there a way to call a function in my directive, but outside of component?

Example:

<ion-input #bar barcodeScanner></ion-input>
<button (click)="bar.start()" type="button">
   <i class="my-icons-scaner"></i>
</button>

Directive:

@Directive({
  selector: '[barcodeScanner]'
})
export class BarcodeScannerDirective implements OnInit {

constructor(private _element: ElementRef) {}

 ngOnInit() {
    const element = this._element.nativeElement
    console.log({ element })
 }

 public start(){
    console.log('start?')
 }

}
like image 250
outrowender Avatar asked Apr 09 '26 21:04

outrowender


1 Answers

Use exportAs property on the Directive decorator like this

Defines the name that can be used in the template to assign this directive to a variable.

directive.ts

@Directive({
  selector: '[barcodeScanner]',
  exportAs: 'barcodeScanner'
})

Then we can access our barcodeScanner instance anywhere in our template:

<ion-input #bar="barcodeScanner" barcodeScanner></ion-input>
<button (click)="bar.start()" type="button">
   <i class="my-icons-scaner"></i>
</button>
like image 103
Chellappan வ Avatar answered Apr 11 '26 18:04

Chellappan வ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!