angular 2 custom directive OnInit

Viewed 13890

i'm just getting starting on Angular 2 and i'm encountering the following problem.

Below is a simple custom directive which is supposed to color the font green. However in the ngOnInit, it can't access the string "defaultColor", the console.log returns "undefined".

Any clue why?

Cheers!

import {Directive, ElementRef, OnInit} from 'angular2/core';

@Directive({
    selector: '[myHighlight]'
})

export class HighlightDirective implements OnInit{
    private elRef:ElementRef;
    private defaultColor: 'green';

    constructor(elRef:ElementRef){
        this.elRef = elRef;
    }

    ngOnInit():any {
        this.elRef.nativeElement.style.color = this.defaultColor;
        console.log(this.defaultColor)
    }

}
2 Answers
Related