The following directive should hide an element.
import {Directive, ElementRef, Input, Renderer2} from '@angular/core';
import {el} from "@angular/platform-browser/testing/src/browser_util";
// Directive decorator
@Directive({ selector: '[hide-me]' })
// Directive class
export class MyHiddenDirective {
// @Input() hideme:boolean;
constructor(public el: ElementRef, public renderer: Renderer2) {
// Use renderer to render the element with styles
renderer.setStyle(el.nativeElement, 'display', 'none');
}
}
I am using it as follows:
<div>Name: <input hide-me #myint type="text" name="name" [(ngModel)]="this.name" /> You entered {{this.name}} {{myint.hasAttribute('required')}}</div>
Why does selector: '[hide-me]'
work but selector: 'hide-me'
doesn't? I do not use square brackes when I use the Directive <input hide-me...
and not <input [hide-me]...
. In Components, we generally specify selector without square brackets. Why do I need to specify square brackets in Directives?