我用的是角1.5.x和TypeScript。对于访问远程API,我使用remote。总之,这是我的场景:
我的API有以下资源http://localhost:53384/api/timezones。发送带有谓词GET的请求到该url返回一个JSON数组:
[
{
"code":"Dateline Standard Time",
"name":"(UTC-12:00) International Date Line West"
},
{
"code":"UTC-11",
"name":"(UTC-11:00) Coordinated Universal Time-11"
},
{
"code":"Hawaiian Standard Time",
"name":"(UTC-10:00) Hawaii"
}
]现在在我的客户端AngularJs应用程序中使用TypeScript:
餐厅配置为 restangularProvider: restangular.IProvider
restangularProvider.setBaseUrl("http://localhost:53384/api");客户端的TimeZone对象表示形式与类型记录
module app.blocks {
"use strict";
export class TimeZone {
public code: string;
public name: string;
}
}Factory(restangular.IService)包装wrap all 的资源
module app.services {
factory.$inject = ["Restangular"];
function factory(restangular: restangular.IService): restangular.IElement {
return restangular.all("timezones");
}
angular
.module("app.services")
.factory("app.services.TimeZonesRestangular", factory);
}服务,它使用包装其uses角功能,并将链接的承诺返回给以异步方式请求时区的人
module app.services {
"use strict";
export interface IStaticDataService {
getTimeZones(): ng.IPromise<app.blocks.TimeZone[]>;
}
class StaticDataService implements IStaticDataService {
constructor(private timeZonesRestangular: restangular.IElement) {
}
public getTimeZones(): ng.IPromise<blocks.TimeZone[]> {
return this.timeZonesRestangular.getList()
.then((timeZones: blocks.TimeZone[]) => {
return timeZones;
}, (restangularError: any) => {
throw "Error retrieving time zones. Status: " + restangularError.status;
});
}
}
factory.$inject = ["app.services.TimeZonesRestangular"];
function factory(timeZonesRestangular: restangular.IElement): IStaticDataService {
return new StaticDataService(timeZonesRestangular);
}
angular
.module("app.services")
.factory("app.services.StaticDataService", factory);
},最后在控制器中,使用服务异步地获取“时区”,我有这样的语句
//..other controller things not relevant for this sample
this.staticDataService.getTimeZones()
.then((timeZones: blocks.TimeZone[]) => {
this.timeZones = timeZones;
});有两个问题:
tsd install restangular --resolve --save一起安装)告诉我,getTimeZones()方法中的successCallback是一个promiseValue: any[],这很好,因为它确实是一个数组。我认为它将是一个TimeZone[]数组,并且类型记录编译正确,因为它接受any[],但是当调试器我看到successCallback承诺值时,它不是TimeZone[]数组。它具有我所期望的特性(code和name),但是它还有许多其他的东西--restangular。数组中的一个对象看起来如下(加上一些函数):
{“代码”:“日期标准时间”、“名称”:“(UTC-12:00)国际日期线西”、“路由”:“时区”、"reqParams":null、"restangularized":true、"fromServer":true、"parentResource":null、"restangularCollection":false }
按照https://github.com/mgonto/restangular/issues/150的说法,我的回答似乎是“重新定位的”。对像我这样新来的人的可怕描述。在What角类型定义中,我应该使用什么接口来表示restangularized TimeZone[] 的数组谢谢。
发布于 2016-02-16 21:53:39
在深入研究之后,我发现处理这一问题的最佳方法是期望类型restangular.ICollection (它继承自IService和Array<any>)的允诺值,这样我就可以像这样重新定位响应:
public getTimeZones(): ng.IPromise<blocks.TimeZone[]> {
return this.timeZonesRestangular.getList()
.then((restangularizedTimeZones: restangular.ICollection) => {
return restangularizedTimeZones.plain();
}, (restangularError: any) => {
throw "Error retrieving time zones. Status: " + restangularError.status;
});
}现在一切似乎都很好,而且反应确实是TimeZone[]的承诺。
https://stackoverflow.com/questions/35443440
复制相似问题