0xffcourse
0xffcourse

Reputation: 379

Cannot we initialize a variable in constructor as follows? [Angular2]

I just started learning angular-2. The string "Pikolo" does not show up in the end-html, when the class appcomponent is as follows:

export class AppComponent {
    mideos:String;
    construtor(){
        this.mideos = "Pikolo";
    }
}

I do not think there is a problem with passing the variable thru components and/or binding with html because the value appears, when I change the code as follows:

export class AppComponent {
    mideos:String = "Pikolo";
    construtor(){

    }
}

I am really confused. Thanks in advance.

Upvotes: 2

Views: 3741

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

Angular2 Component's constructor specifically for DI purposes,Initializing a property in the constructor allows you to leverage constructor parameters when you're initializing the property.

There is no difference in generated JS between the two.

EDIT: your code has a typo error

export class AppComponent {
    mideos:String;
    constructor(){
        this.mideos = "Pikolo";
    }
}

Upvotes: 4

Related Questions