0

I am trying to read the following in the root component of an angular app

 <app-root [something]="'sometingvalue'">Loading...</app-root>

I have the angular component the following way

export class AppComponent {
    something: string = 'abcd';
    title: string = 'app works!';
    elm: ElementRef;


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

    ngOnInit() {
        this.something = this.elm.nativeElement.getAttribute('something');
    }
}

However I always get null on the ngOnInit even, how do I read this value. I cannot use @input since this is the root component.

user3547774
  • 1,621
  • 3
  • 20
  • 46

1 Answers1

1

Seems like its only possible to pass strings into those attributes!

See my plunker: https://plnkr.co/edit/ThPAdFz2laP778JuEYb7?p=preview

For your info:

<p [attribute-name]="expression-here"></p>
<p attribute-name="string-here"></p>

As you like to store a string, use the NON-expression way!

slaesh
  • 16,659
  • 6
  • 50
  • 52