Specify @Input() parameter for Angular root component/module

jenson-button-event picture jenson-button-event · Apr 21, 2017 · Viewed 9k times · Source

I have 3 root components that get bootstrapped by the root AppModule.

How do you go about specifying an @Input() parameter to one of those components?

Neither

<app-modal [id]="'hard-coded value'"></app-modal>
<app-modal [id]="constantExportedByAppmodule"></app-modal>

are picked up by AppModalComponent:

@Input() id: string;

It is undefined.

Answer

Gr&#233;gory Gossart picture Grégory Gossart · Apr 21, 2017

As far as I know, you can not pass @Input() to a bootstraped component.

But you can use another way to do that - pass the value as an attribute.

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');
    }
}