con*_*sed 5 typescript angular2-http angular
我正在玩Angular2,并希望有人可以提供一些如何实现这一目标的建议;
例如,如果我的模型目前对于员工来说是这样的:
export class Employee {
constructor(
public firstName: string,
public lastName: string,
public email: string,
public days: string,
public hours: string,
){}
}
Run Code Online (Sandbox Code Playgroud)
我希望将天/小时放入他们自己的物体中,怎么可能实现呢?
(比如..)
public availability: {
public days: string,
public hours: string
},
Run Code Online (Sandbox Code Playgroud)
然后http get请求保持不变如下所示?
getEmployees() {
return this.http.get('...')
.map((response: Response) => {
const data = response.json().obj;
let objs: any[] = [];
for(let i = 0; i < data.length; i++) {
let employee = new Employee(
data[i].firstName,
data[i].lastName,
data[i].email,
data[i].availability.days,
data[i].availability.hours
);
objs.push(employee)
}
return objs
})
}
Run Code Online (Sandbox Code Playgroud)
只是为了澄清,我希望我的请求返回类似的东西;
var obj = {
firstName: "test",
lastName: "test",
email: "test",
availability: {
days: "test",
hours: "test"
}
}
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮忙!我试图环顾四周,但没有遇到任何有用的东西.
Mic*_*ael 13
像这样的东西
export class Employee {
constructor(
public firstName: string,
public lastName: string,
public email: string,
public availability: Availability // refer to type Availability below
){}
}
export class Availability {
constructor(
public days: string,
public hours: string
){}
}
Run Code Online (Sandbox Code Playgroud)
Http get请求应保持不变,然后更改您创建新员工实例的方式
let employee = new Employee(
data[i].firstName,
data[i].lastName,
data[i].email,
new Availability(
data[i].availability.days,
data[i].availability.hours
)
);
Run Code Online (Sandbox Code Playgroud)