1

I have two angular methods which checking roles and permission.If one of these condition true that value assigns to managePermsission boolean property in component. These are two methods in service:

checkProjectManagePermission(): Observable<boolean> {
    const currentLoggedUser = this.authService.getCurrentLoggedInUser();
    const reqUrl = `${currentLoggedUser.mPortalWebApi}${CONST.ctrlUrl.projects}/check-project-manage-permission`;

    return this.http.get(reqUrl).pipe(
        map((response: any) => response),
        catchError((error) => _throw(error)),
    );
}

checkProjectUserRoles(): Observable<boolean> {
    const currentLoggedUser = this.authService.getCurrentLoggedInUser();
    const reqUrl = `${currentLoggedUser.mPortalWebApi}${CONST.ctrlUrl.projects}/${this.projectId}/check-project-user-roles`;

    return this.http.get(reqUrl).pipe(
        map((response: any) => response),
        catchError((error) => _throw(error)),
    );
}

and this is code in component. I want to set property projectManagePermssions on true if some of this mehod return true, but second method set projectManagePermssions to false always altough service response return true value.

projectManagePermissions = false;

 ngOnInit() {       
    this.showLoader = true;
   
    this.checkProjectManagePermission();       
  
   if (!this.projectManagePermissions)         
      this.checkProjectUserRoles();                                 
}

private checkProjectManagePermission() {
    this.projectManagementService.checkProjectManagePermission()
        .pipe(takeUntil(this.observablesDispose$))
        .subscribe(
            (response) => {              
                this.projectManagePermissions = response                    
            }
        )
}

private checkProjectUserRoles() {
    return this.projectManagementService.checkProjectUserRoles()
        .pipe(takeUntil(this.observablesDispose$))
        .subscribe(
            (response) => {                  
                this.projectManagePermissions = response                
            }
        )
}

What I missed here in code ? Thanks in advance !

2 Answers 2

1

try this.

projectManagePermissions = false;
    
     ngOnInit() {       
        this.showLoader = true;
        this.checkProjectManagePermission();                                       
    }
    
    private checkProjectManagePermission() {
        this.projectManagementService.checkProjectManagePermission()
            .pipe(takeUntil(this.observablesDispose$))
            .subscribe(
                (response) => {              
                    this.projectManagePermissions = response;
                    if (!this.projectManagePermissions)  {       
                            this.checkProjectUserRoles();  
                    }                    
                }
            )
    }
    
    private checkProjectUserRoles() {
        return this.projectManagementService.checkProjectUserRoles()
            .pipe(takeUntil(this.observablesDispose$))
            .subscribe(
                (response) => {                  
                    this.projectManagePermissions = response                
                }
            )
    }
0
0

This is an async issue. ngInit completes before checkProjectManagePermission() does. Subscribe to checkProjectManagePermission() inside ngOnInit.

To ensure you don't get a memory leak, assign checkProjectManagePermission() to a variable, and then call unsubscribe on it inside ngOnDestroy.

2
  • It's still does not work..I put your suggestion. I noticed another strange thing when I go to another link and come back it is a good value if I repeat or refresh the page again it is false @Bytech
    – redux17
    Oct 13, 2020 at 23:24
  • This may help dev.to/jwp/angular-why-doesn-t-my-data-show-up-4efm
    – JWP
    Oct 14, 2020 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.