Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from config.json in Angular 2

Tags:

angular

I want to load all url from a config file. I've created json file named 'config.json", ConfigurationService.ts to get the URL by key. But I cannot get the URL by key.

config.json:

{
"loginUrl": "http:example/login",
"verifyUrl": "http:example/verify"
}

ConfigurationService.ts:

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Observable';

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

private result: Object;

getConfiguration(key) {
    return this.http.get('./app/config/config.json').map(res => {
        res.json();
        this.result = res._body;
        return this.result[key]; //returns 'undefined', but when I return only 'this.result'  it shows me all json data {...}
    });
}
}

part of auth_service.ts:

private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));

How do I receive only the loginUrl for example?

like image 696
Serhiy Avatar asked Mar 22 '26 23:03

Serhiy


1 Answers

I think what you're looking for is this:

getConfiguration(key) {
    return this.http.get('./app/config/config.json').map(res => {
        this.result = res.json();
        return this.result[key];
    });
}
like image 150
rinukkusu Avatar answered Mar 25 '26 14:03

rinukkusu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!