0

I have an Angular 2 application that needs to access to a JSON variable called "configuration" which has been passed as a "POST" parameter to the application itself.

With Angularjs, I used ng-init this way:

<html lang="it" ng-app="myApp" ng-init='configuration=<%- configuration %>'>

And I could access to that variable using $scope.configuration. But what about Angular 2? The only way I found to make this variable usable from the application is putting it inside a not-visible div, inside index.html page.

<body><div id="getConfig"><%- configuration %></div>
<app>Loading...</app>
</body>

And inside app.component.ts...

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app',
    templateUrl: './app-fe/app.component.html'
})

export class AppComponent {

    private title:string = 'Hello World';
    private configuration = document.getElementById("getConfig").innerHTML;    

    constructor() {
    console.log('=== app started ===');
    console.log(this.configuration);
  };
}

But something tells me this is not the best way...

B. Ciervo
  • 135
  • 4
  • 16
  • You can use APP_INITIALIZER, http://stackoverflow.com/questions/37611549/how-to-pass-parameters-rendered-from-backend-to-angular2-bootstrap-method – silentsod Dec 06 '16 at 16:16
  • Great! I used this suggestion: http://stackoverflow.com/questions/33152914/angular-2-external-inputs And it worked! But any other ideas are accepted ;) – B. Ciervo Dec 06 '16 at 16:47

0 Answers0