小编moe*_*774的帖子

如何将JSON对象解析为TypeScript对象

我目前正在尝试将我收到的JSON对象转换为具有相同属性的TypeScript类,但我无法使其工作.我究竟做错了什么?

员工类

export class Employee{
    firstname: string;
    lastname: string;
    birthdate: Date;
    maxWorkHours: number;
    department: string;
    permissions: string;
    typeOfEmployee: string;
    note: string;
    lastUpdate: Date;
}
Run Code Online (Sandbox Code Playgroud)

员工字符串

{
    "department": "<anystring>",
    "typeOfEmployee": "<anystring>",
    "firstname": "<anystring>",
    "lastname": "<anystring>",
    "birthdate": "<anydate>",
    "maxWorkHours": <anynumber>,
    "username": "<anystring>",
    "permissions": "<anystring>",
    "lastUpdate": "<anydate>"
    //I will add note later
}
Run Code Online (Sandbox Code Playgroud)

我的尝试

let e: Employee = new Employee();

Object.assign(e, {
    "department": "<anystring>",
    "typeOfEmployee": "<anystring>",
    "firstname": "<anystring>",
    "lastname": "<anystring>",
    "birthdate": "<anydate>",
    "maxWorkHours": 3,
    "username": "<anystring>",
    "permissions": "<anystring>",
    "lastUpdate": "<anydate>"
});

console.log(e);
Run Code Online (Sandbox Code Playgroud)

链接到Typescript …

json typescript angular

36
推荐指数
4
解决办法
11万
查看次数

如何在Angular 2中"链接"两个单独的可观察量

我正在使用Angular2开发一个Web应用程序,我从服务器获取数据时遇到了一些问题.

 import ...

 @Component({
 ...

 })
 export class EmployeeManagementTableComponent implements OnInit, OnDestroy{

private employees: Employee[];
private departments: SelectItem[] = [];
private selectedDepartment: string;
private columns: any[];
private paramSub: any;
private employeesSub: any;
private departmentSub: any;


constructor(private employeeManagementService: EmployeeManagementService, 
            private route: ActivatedRoute, 
            private router: Router,
            private ccs: ComponentCommunicatorService,
            private logger: Logger) { }

ngOnInit(){

    this.columns = [
     ...
    ];

    //ccs is just a service for storing/getting app wide infomation

    this.selectedDepartment = this.ccs.getSelectedDepartment();
    this.getDepartments();
    this.getEmployees(this.selectedDepartment);

    ...
}

ngOnDestroy(){
    /*this.employeesSub.unsubscribe();
    this.departmentDub.unsubscribe();*/
}

getDepartments(){ …
Run Code Online (Sandbox Code Playgroud)

observable typescript angular

5
推荐指数
1
解决办法
1万
查看次数

如何将可观察到的数组形式映射到另一个数组

我目前正在使用this.route.url.subscribe(params=>...我的路线参数做一些事情。我想检查某些参数是否在订阅的param数组中。但是此数组包含URLSegments我只想从中检查path属性的数组。有什么办法可以与map操作员重新映射?
我尝试像这样重新映射整个数组,this.route.url.map(x=>x.path)但由于数组本身没有no,因此无法正常工作path。我想念什么吗?

javascript rxjs typescript angular2-observables angular

1
推荐指数
1
解决办法
1287
查看次数