-2
I created this directive inside a directive folder
app
|_ diretivas
|_ click-outside.directive.ts
My directive is like this:
import { Directive, Output, EventEmitter, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[clickOutside]'
})
export class ClickOutSideDirective {
@Output () clickOutside = new EventEmitter<void>()
constructor(private elementRref: ElementRef) { }
@HostListener('document:click', ['$event.target'])
public onClick( target ){
const clickedInside = this.elementRref.nativeElement.contains( target )
console.log(clickedInside)
if( !clickedInside ){
this.clickOutside.emit()
}
}
}
I call it that in my component
<div class="form-group has-feedback pull-right" (clickOutSide)="onClickOutside()">
<input type="text" class="form-control" #iptSearch placeholder="Digite sua pesquisa" (input)="searchTextPendente($event.target.value)" [@campoInput]="campoState">
<span class="fa fa-search form-control-feedback"></span>
</div>
In the component is like this:
onClickOutside() {
console.log('onClickOutside');
this.click++;
}
But it doesn’t work... How to proceed to tidy directives only in a specific folder?
That one is the code on Stackblitz.
Remembering that if I put out the folder diretiva
, works
[EDIT]
I was able to make the console.log show in the directive class the value, but if it is false it was to send, but it does not execute anything in the component where I call the function (clickOutSide)="onClickOutside()"
If I put out the folder, it works.. but I’d like to organize
– adventistaam
But shows some error in the console?
– LeAndrade
Shows no error
– adventistaam
Conssegui make the console the value Boolean but in the component where calls I am not able to perform the function
– adventistaam
I’ll confess that I don’t quite understand what you want to do with this directive function, but try to follow from this example here: https://stackblitz.com/edit/angular-directive
– LeAndrade
I tried, Stackblitz works, but in my project, Emit doesn’t work
– adventistaam
This is my git project: the html, the ts and the directive
– adventistaam