Loading data from angular2 configuration file

I created a GUI to retrieve data from a remote server. Now I want to try to save the settings of this GUI in the configuration file in order to change the file settings.

I do not know how to create a configuration file in Angular2. I tried to find links on how to create and use the configuration file, but I just found it for Java and C #. Does it exist in Angular2?

+4
source share
3 answers

To do this, you can use the JSON configuration file and upload it using HTTP. In this case, you can download your application asynchronously to wait for the configuration data to load. Here is an example:

let appProviders = [ HTTP_PROVIDERS, ConfigurationService ];

var app = platform(BROWSER_PROVIDERS)
  .application([BROWSER_APP_PROVIDERS, appProviders]);

let service = app.injector.get(ConfigurationService);
service.getConfiguration().flatMap((configuration) => {
  var configurationProvider = new Provider('configuration', { useValue: configuration });
  return app.bootstrap(AppComponent, [ configurationProvider ]);
}).toPromise();

A class ConfigurationServicecould be something like this:

@Injectable()
export class ConfigurationService {
  constructor(private http:Http) {
  }

  getConfiguration() {
    return this.http.get('config.json').map(res => res.json());
  }
}

See this question for more information:

+8
source

You cannot create a file in Angular. Angular runs in the browser, and the browser restricts access to the local file system.

Instead, you should send data to the server so that the server can create a file for the client.

-1
source

, :

  • , ../ REST , PUT GET.

  • Store on the client side using either localStorage or IndexedDB. The obvious drawback of this approach is that the setting will be isolated in the machine / browser where it is created. unavailable if the user enters a name from another device.

-2
source

All Articles