steky
steky

Reputation: 101

Loading data from config file angular2

I have created a GUI for retrieving data from a remote server. Now I want to try and save the settings of this GUI into a config file, so that the file settings could be changed.

I have no idea how to create a config file in Angular2. I tried to looking into links on how to create and use a config file but I just found it for Java and C#. Does it exists in Angular2?

Upvotes: 4

Views: 9095

Answers (3)

cesarpachon
cesarpachon

Reputation: 1213

as you want to edit the GUI configuration, you have only two choices:

  • store the settings in server side and provide retrieval and saving mechanisms, i.e: a /settings REST endpoint that store changes on PUT and retrieve configuration on GET.

  • store at client side, using either localStorage or IndexedDB. the obvious downside of this approach is that the setting will be isolated in the machine/browser where it is created. not accessible if the user login from another device.

Upvotes: -2

Thierry Templier
Thierry Templier

Reputation: 202176

You could use a JSON configuration file for this and load it using HTTP. In this case, you could bootstrap your application asynchronously to wait for this configuration data to be loaded. Here is a sample:

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();

The ConfigurationService class could be something like that:

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

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

See this question for more details:

Upvotes: 8

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657308

You can't create a file in Angular. Angular runs in the browser and the browser limits access to the local file system.

What you want instead is to send data to the server for the server to create the file for the client.

Upvotes: -1

Related Questions