2
votes

I have made a heatMap Attribute directive which is not receiving the values when passed while calling.

HeatMap Directive:

import {Directive, ElementRef, Input, Renderer, OnInit} from "@angular/core";

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

export class HeatMapDirective{

    @Input('startVal') public startVal: string;

    constructor(private el: ElementRef, private renderer: Renderer) {
        this.processHeatMap();
    }

    processHeatMap() {
     console.log(this.startVal); // prints undefined.
    }
}

HTML Element:

    <td class="pad-10" [startVal]="'25'" HeatMap> 25 </td>

I have a scenario where i need to pass both string and numeric values but both are not working. Am I missing something?

2
are you getting any error ? - ranakrunal9
No Errors! console.log(this.startVal); // prints undefined. - Sumit Agarwal
Check angular2 lifecycle hooks. There are no initialized @Input values in constructor - yurzui

2 Answers

0
votes

Change your html as:

<td class="pad-10" [startVal]="'25'" HeatMap> 25 </td>
0
votes

Input values are not initialized yet when addressing them from the constructor, address them from ngOnInit() like that :

ngOnInit() {
  this.processHeatMap();
}