I have component with tabs (ProfileComponent
is parent for others). I want to load data from server using services - roles and projects - in ProjekteingangComponent
.
I wrote for this functions for loading data (loadRoles()
and loadProjects()
) and call it on ngOnInit()
, but the problem is that value
not sets for this.roles
and this.projects
. Here and here is output to console in browser.
As you see in output value
in loadRoles()
and loadProjects()
writes after the error. How can I improve this and set this.projects
and this.roles
in ngOnInit()
correctly?
projekteingang.component.ts:
import {Component, Input, OnInit} from '@angular/core';
import {ProfileComponent} from '../profile.component';
import {UserOfferMapping} from "../../../_models/UserOfferMapping";
import {UseroffermappingService} from "../../../_services/useroffermapping.service";
import {Userproject} from "../../../_models/Userproject";
import {Userrole} from "../../../_models/userrole";
import {UserprojectService} from "../../../_services/userproject.service";
import {UserroleService} from "../../../_services/userrole.service";
import {ProjectUtils} from "../../../utils/ProjectUtils";
@Component({
selector: 'app-projekteingang',
templateUrl: './projekteingang.component.html',
styleUrls: ['./projekteingang.component.css']
})
export class ProjekteingangComponent implements OnInit {
public userOfferMapping: any[];
public userId: number;
public offerDataSource: any[] = [];
public projects: Userproject[] = [];
public roles: Userrole[] = [];
constructor(
private profileComponent: ProfileComponent,
private useroffremappingService: UseroffermappingService,
private userProjectService: UserprojectService,
private userRoleService: UserroleService,) {
}
loadRoles() {
this.userRoleService.getAllRoles().toPromise().then((value) => {
console.log("value roles");
console.log(value);
this.roles = value;
});
}
loadProjects(){
this.userProjectService.getAllProjects().toPromise().then((value) => {
console.log("value projects");
console.log(value);
this.projects = value;
});
}
ngOnInit() {
this.userId = this.profileComponent.currentUser.id;
this.loadProjects();
this.loadRoles();
console.log("roles ngoinit");
console.log(this.roles);
console.log("projects ngoinit");
console.log(this.projects);
this.loadUserOfferMappings();
}
findProjectByID(id) {
let project = null;
this.projects.map(elem => {
if (elem.id == id) {
project = elem;
}
})
return project;
}
findRoleByID(id) {
let role = null;
this.roles.map(elem => {
if (elem.id == id) {
role = elem;
}
})
return role;
}
loadUserOfferMappings() {
console.log("roles in func");
console.log(this.roles);
console.log("projects in func");
console.log(this.projects);
this.useroffremappingService.getAllUseroffermappingsByUserId(this.userId).subscribe(value => {
console.log("Useroffermapping");
console.log(value);
this.userOfferMapping = value;
value.map(elem => {
console.log(elem);
let project = this.findProjectByID(elem.projectId);
let role = this.findRoleByID(elem.roleId);
console.log(role);
console.log(project);
this.offerDataSource.push({
projectId: elem.projectId,
roleId: elem.roleId,
projectName: project.name,
roleName: role.name,
roleTooltip: role.description
});
})
});
}
get profile() {
return this.profileComponent;
}
}
profile.component.html:
<mat-tab-group>
<-- ... -->
<-- another tabs -->
<-- ... -->
<mat-tab label="Projekt eingang">
<app-projekteingang></app-projekteingang>
</mat-tab>
</mat-tab-group>