3

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?

1 Answer 1

5

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];
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.