【问题标题】:how can i get string json in angular2我如何在 angular2 中获取字符串 json
【发布时间】:2017-09-16 13:19:33
【问题描述】:

在我的 angular2 组件中

keyword_test={};

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

}

console.log(data) 和 console.log(this.keyword_test) 像这样打印正确的数据

                {
                    "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"

                                }
                            ]
                        }
                    ]
                }

但在我的 ngOnInit 中

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

尽管 this.getdata(), this.keyword_test print "Object {}" 我认为没有对象。 是keyword_test 初始化不正确吗? 当我在 getData 函数中打印 console.log(typeof data) 时,结果是字符串... 服务中确实改成json了,不知道为什么。

++这是我的服务

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

}

【问题讨论】:

  • 在keyword_test中手动输入会正常工作。

标签: json angular


【解决方案1】:
 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
}

正确的方法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多