Angularjs 当';时,重新设置三角形并使打字脚本混淆;重新三角形化';响应对象
我正在使用Angular 1.5.x和TypeScript。为了访问远程API,我使用restanglar。作为总结,这是我的设想: 我的API有以下资源Angularjs 当';时,重新设置三角形并使打字脚本混淆;重新三角形化';响应对象,angularjs,typescript,angular-promise,restangular,tsd,Angularjs,Typescript,Angular Promise,Restangular,Tsd,我正在使用Angular 1.5.x和TypeScript。为了访问远程API,我使用restanglar。作为总结,这是我的设想: 我的API有以下资源http://localhost:53384/api/timezones。向该url发送带有动词GET的请求将返回一个JSON数组: [ { "code":"Dateline Standard Time", "name":"(UTC-12:00) International Date Line West" }
http://localhost:53384/api/timezones
。向该url发送带有动词GET的请求将返回一个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:
正在重新启动的重新启动配置提供程序:restanglar.IProvider
使用typescript在客户端表示时区对象
module app.blocks {
"use strict";
export class TimeZone {
public code: string;
public name: string;
}
}
//..other controller things not relevant for this sample
this.staticDataService.getTimeZones()
.then((timeZones: blocks.TimeZone[]) => {
this.timeZones = timeZones;
});
工厂(restangular.IService)包装restangular所有“时区”资源
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);
}
使用TimeZoneRestAngular包装其重新启动语言功能并向以异步方式请求时区的人返回链接承诺的服务
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);
}
有两个问题:
promiseValue:any[]
,这很好,因为它确实是一个数组。我原以为它是一个时区[]数组,typescript可以正确编译,因为它接受any[]
,但在调试时,我看到successCallback承诺值不是一个时区[]数组。它具有我所期望的属性(code
和name
),但它还有许多其他特性。该数组中的对象如下所示(加上一些函数):
据我所知,我的回答似乎已经“重新语言化”。对像我这样刚开始学英语的人的可怕描述。。
我应该使用重新格式化类型定义中的哪个接口来表示重新格式化的时区[]
?谢谢。在进一步挖掘之后,我发现处理此问题的最佳方法是期望类型为
restangular.ICollection
(继承自iSeries
和Array
)的承诺值,以便我可以像这样取消响应的重新语言化:
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;
});
}
public getTimeZones():ng.IPromise{
返回this.timezoneRestAngular.getList()
.then((restangularizedTimeZones:restangular.ICollection)=>{
返回restangularizedTimeZones.plain();
},(restangularError:any)=>{
抛出“检索时区时出错。状态:”+restangularError.Status;
});
}
现在,一切似乎都很好,而人们的反应确实是对时区的承诺
{
"code":"Dateline Standard Time",
"name":"(UTC-12:00) International Date Line West",
"route":"timezones",
"reqParams":null,
"restangularized":true,
"fromServer":true,
"parentResource":null,
"restangularCollection":false
}
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;
});
}