I have a form for ng-select component, which shows the roles of a user, in my Angular 4 project. In that component, first I need to get the user roles and show them as default values. To be able to do that, I am using FormControls in ngOnInit. Because I get the default values from a service, they are loading so slow according to the initialisation of the FormGroup. I am calling both of them in ngOnInit but everytime the FormGroup runs before. So I can't see any of the default values on the screen. I tried to make it async but I couldn't do it.
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.userTC = params['id'];
this.getUserInfo(this.userTC); // This function works second
});
// This part works first
this.form = new FormGroup({
roles: new FormControl(this.defaultRoles)
});
}
getUserInfo(tc) {
this.userService.getUser(tc).subscribe(data => {
if(data) {
let arr = new Array<string>();
for(i = 0; i < data.roles.length; i++) {
switch(data.roles[i]) {
case "admin" :
arr[i] = '1';
break;
case "doktor" :
arr[i] = '2';
break;
case "hasta" :
arr[i] = '3';
break;
case "lab" :
arr[i] = "4";
break;
}
}
this.defaultRoles = arr;
}
}
}