1
<body>
  <app-root data="myData"></app-root>
</body>

I need to retrieve myData that I will manually add at index.html and use it into a component.

How can I retrieve this data?

Thanks.

KevinTale
  • 1,658
  • 3
  • 25
  • 47
  • If you are manually adding it, why not just manually add it into the component in the first place? – Brian Wright Nov 15 '17 at 17:06
  • Possible duplicate of [Specify @Input() parameter for Angular root component/module](https://stackoverflow.com/questions/43543928/specify-input-parameter-for-angular-root-component-module) – Sangwin Gawande Nov 16 '17 at 05:39

1 Answers1

2

I believe this answer has what you need:

https://stackoverflow.com/a/43544999/3581932

Use @Attribute (for Angular 4)

index.html :

<my-app myAttribute="myAttributeValue">
    <div>Loading...</div>
</my-app>

app.component.ts :

@Component({
    selector: 'my-app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css']
})
export class AppComponent {
    constructor(private elementRef:ElementRef) {
        let myAttribute = this.elementRef.nativeElement.getAttribute('myAttribute');
    }
}

[Copied directly from above post]

Brian Wright
  • 827
  • 6
  • 9