将来自 oracle 的 JSON 响应转换为 angular 2 变量
Converting JSON response from oracle to angular 2 variable
我正在尝试从 oracle 数据库中获取数据并显示它。我已经使用 express api 和 node 创建了一个服务,并且能够成功 运行 它。
我已经创建了 angular 服务来获取数据,并将其分配到 angular 组件中,但我无法将 JSON 响应映射到 angular 变量。请检查下面的代码并帮助我在 angular component.ts 中需要更改的内容以及如何在 html.
中使用该变量
JSON 来自 Oracle 数据库的数据:
[{"COUNT":27}]
angular 服务:
getCount(): Promise<String[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data)
.catch(this.handleError);
}
angular component.ts
dataGS : String[];
getData(): void {
this.dataService
.getCount()
.then(dataGS => this.dataGS = dataGS);
怎么样
getCount(): Promise<String[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => {
response.json().map(function(item) {
return item['COUNT'];
})
})
.catch(this.handleError);
}
在你的html
<div><p *ngFor="let d of dataGS ">{{d}}</p></div>
您可以在 angular 中的 template bindings 的帮助下实现此目的。
例如-
在您的组件中,您在从 db
获取数据后收到 this.dataGS = [{"COUNT":27}]
。然后在你的 html template
上,你可以在 Interpolation 的帮助下显示它,比如
<div>{{dataGS[0].count}}</div>
您的回复没有 属性 data
,它只是一个数组。所以而不是:
.then(response => response.json().data)
做:
.then(response => response.json())
现在你将得到你的阵列。然后按照 jitender 的建议,您可以重复您的回复:
<div><p *ngFor="let d of dataGS ">{{d}}</p></div>
我正在尝试从 oracle 数据库中获取数据并显示它。我已经使用 express api 和 node 创建了一个服务,并且能够成功 运行 它。 我已经创建了 angular 服务来获取数据,并将其分配到 angular 组件中,但我无法将 JSON 响应映射到 angular 变量。请检查下面的代码并帮助我在 angular component.ts 中需要更改的内容以及如何在 html.
中使用该变量JSON 来自 Oracle 数据库的数据:
[{"COUNT":27}]
angular 服务:
getCount(): Promise<String[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data)
.catch(this.handleError);
}
angular component.ts
dataGS : String[];
getData(): void {
this.dataService
.getCount()
.then(dataGS => this.dataGS = dataGS);
怎么样
getCount(): Promise<String[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => {
response.json().map(function(item) {
return item['COUNT'];
})
})
.catch(this.handleError);
}
在你的html
<div><p *ngFor="let d of dataGS ">{{d}}</p></div>
您可以在 angular 中的 template bindings 的帮助下实现此目的。
例如-
在您的组件中,您在从 db
获取数据后收到 this.dataGS = [{"COUNT":27}]
。然后在你的 html template
上,你可以在 Interpolation 的帮助下显示它,比如
<div>{{dataGS[0].count}}</div>
您的回复没有 属性 data
,它只是一个数组。所以而不是:
.then(response => response.json().data)
做:
.then(response => response.json())
现在你将得到你的阵列。然后按照 jitender 的建议,您可以重复您的回复:
<div><p *ngFor="let d of dataGS ">{{d}}</p></div>