I have written a Directive and a Component, I need to pass the value from the Component to the directive and accordingly hide the element. In simple words I'm trying to create an directive similar to ng-show and ng-hide of Angular1 in Angular2.
headermenu.component.ts
import {Component} from 'angular2/core';
import {DataService} from './data-service.service';
import {ShowHeaderDirective} from './show-header.directive';
@Component({
selector: 'header-menu',
template: `
<header class="login-header">
<div class="header-top">
<div class="container">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div>
<ul class="nav navbar-nav navbar-right">
<li [showHeader]="dispFlag"><a href="javascript:void(0)">Mr. Abc!</a></li>
<li><span>|</span></li>
</ul>
</div>
</div><!--/.container-fluid -->
</nav>
</div>
</div>
</header>
`,
directives: [ShowHeaderDirective],
providers:[DataService]
})
export class HeaderComponent {
dispFlag;
constructor(dataService: DataService){
this.dispFlag=dataService.headerDisplayFlag;
}
}
show-header.directive.ts
import {Directive, ElementRef, Renderer, Input} from 'angular2/core';
@Directive({
selector: '[showHeader]'
})
export class ShowHeaderDirective{
private _el:HTMLElement;
constructor(private el: ElementRef, private renderer: Renderer){
debugger;
/* alert(this.el.nativeElement.attributes.showheader.value);
if(this.el.nativeElement.attributes.showheader.value=="false"){
this.el.nativeElement.style.display="none";
} */
}
}
I need to pass the value of dataFlag value which is collected from dummy service and send it to directive and directive will accordingly show/hide the element.
Note: dataFlag - hold the value true/ false.
Currently I am not able to get any output from it so commented the code.