0

This is my start page, it is rendered on the backend (instead of 'propval' there will be a variable containing the json-configuration):

<%- include('template/head'); %>
<application [userdata]='"propval"'>
    Loading...
</application>
<%- include('template/foot'); %>

This is my component that handles the 'application' selector:

@Component({
    moduleId: module.id,
    selector: 'application',
    templateUrl: './app.component.html',
    providers:[
        ProgressBar
    ]
})
export class AppComponent implements OnInit {

    public userdata;

    constructor(public progressBar :ProgressBar, private router: Router) {
        console.log("constructor",this.userdata);
    }

    public async ngOnInit() {
        console.log("ngOnInit",this.userdata);
    }

}

As a result, in the console I get:

constructor undefined
ngOnInit undefined

How to read userdata property? Perhaps this is not will done in AppComponent, since application selector is not rendered inside the app.component.html. application selector rendered on the backend.

Ajit Soman
  • 3,926
  • 3
  • 22
  • 41
sanu
  • 529
  • 10
  • 30

1 Answers1

1

The question Vega shared points out that bootstrap and entry components cannot use input bindings. (I did not find the source code, but by reasoning that an entry component should not have a parent, it makes sense that they would not use input bindings.)

The accepted answer uses ElementRef to get the data attribute, that approach should work for your case as well.

import {ElementRef} from '@angular/core';

constructor(private _ref: ElementRef) {}

ngOnInit() {
    this.userdata = this._ref.nativeElement.getAttribute('userdata');
}

Sample plunker

Other solutions that might work for you or others: configure an InjectionToken (doc) or a use a service.

stealththeninja
  • 3,576
  • 1
  • 26
  • 44