김진창
김진창

Reputation: 105

how can i get string json in angular2

in my angular2 component

keyword_test={};

getData() {
    this.service.getData()
        .subscribe(
            data => {
                this.keyword_test = data
                console.log(data);
                console.log(this.keyword_test);
            });

}

console.log(data) and console.log(this.keyword_test) print right data like this

                {
                    "caption": "folder0",
                    "type": "folder",
                    "subnodes": [
                        {
                            "caption": "folder1",
                            "type": "folder",
                            "subnodes": [
                                {
                                    "caption": "keyword1",
                                    "type": "keyword",
                                    "search_filter_expression": "sfe1"
                                },
                                {
                                    "caption": "keyword2",
                                    "type": "keyword",
                                    "search_filter_expression": "sfe2"
                                }
                            ]
                        },
                        {
                            "caption": "folder2",
                            "type": "folder",
                            "subnodes": [
                                {
                                    "caption": "keyword3",
                                    "type": "keyword",
                                    "search_filter_expression": "sfe3"

                                },
                                {
                                    "caption": "keyword4",
                                    "type": "keyword",
                                    "search_filter_expression": "sfe4"

                                }
                            ]
                        }
                    ]
                }

but in my ngOnInit

 ngOnInit() {
    this.getData();
    console.log(this.keyword_test);
}

despite the this.getdata(), this.keyword_test print "Object {}" i think none object. Is the keyword_test initialized incorrectly? when i print console.log(typeof data) in getData function, result is string... I did change it to json in service, but I do not know why.

++and this is my service

@Injectable()
export class keywordService {
    private API_URI: string = 'MYAPIURL';

    constructor(private http: Http) {
    }

    getData() {
        return this.http.get(this.API_URI, {body: ""})
            .map(res => {res.json();
        });
    }

}

Upvotes: 0

Views: 279

Answers (1)

Jorawar Singh
Jorawar Singh

Reputation: 7621

 ngOnInit() {
    this.getData(); // this execute but data arrives with call back
    console.log(this.keyword_test); //this execute before data has arrived and thats why it is not printing the result from success call
}

Correct way

this.service.getData()
   .subscribe(
       data => {
          this.keyword_test = data
          console.log(data);
          console.log(this.keyword_test);
});

Upvotes: 0

Related Questions