Reputation: 11
I am starting working with Angular, more specifically v5, but I am stuck in something.
One of my requirements is to have a way to configure and manually bootstrap the application from an HTML page. I have made some researches and I found that Angular v1.x had a way of doing that. Example:
Configuration:
var config = { //my configuration object }
myApp.config(['myConfigProvider', 'myBaseFeaturesProvider', function(myConfigProvider, myBaseFeaturesProvider) {
//make the necessary configuration with the config object
}]);
And then run the application, inserting it in a specific location on the page:
myApp.run(['$templateCache', function($templateCache) {
angular.element('#myId').html($templateCache.get('path/to/app/html/app.html'));
}]);
My question is how can I do it in Angular 5? Currently I set up the application with angular-cli, but I did not find anything specifically to the point I mentioned.
Upvotes: 0
Views: 558
Reputation: 11
After a lot of digging and talks I could find out an answer for my question. I am adding here the steps I made to achieve the desired solution:
main.ts
file import a global configuration class that contains my configuration object and desired logic to set and get attributes:import { Configuration } from './app/bootstrap/configuration';
main.ts
file, create an object that will handle the configuration and make the Angular bootstrap as follows:window['myConfigurationFunction'] = {
config: function (config) {
// Set the configuration as a class attribute
Configuration.features = config;
// Bootstrap Angular
platformBrowserDynamic().bootstrapModule(myApp);
}
}
index.html
:import { Injectable } from '@angular/core';
import { Configuration } from './app/bootstrap/configuration';
@Injectable()
export class MyService {
constructor() { }
log() {
console.log('My property', Configuration.features.myProperty);
}
}
index.html
, call the configuration function defined previously in the main.ts
file:[...]
<my-app></my-app>
<script>
let configExample = {
myProperty: 'Hello World!'
}
myConfigurationFunction.config(configExample);
</script>
Upvotes: 1