简体   繁体   English

Angular ngOnInit() 属性不会更新值

[英]Angular ngOnInit() property don't upate value

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.我有两个角度方法来检查角色和权限。如果这些条件之一为真,则该值分配给组件中的 managePermsission 布尔属性。 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.如果此方法中的某些方法返回 true,我想将属性 projectManagePermssions 设置为 true,但第二种方法始终将 projectManagePermssions 设置为 false,尽管服务响应返回 true 值。

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 !提前致谢 !

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                
                }
            )
    }

This is an async issue.这是一个异步问题。 ngInit completes before checkProjectManagePermission() does. ngInit 在 checkProjectManagePermission() 之前完成。 Subscribe to checkProjectManagePermission() inside ngOnInit.在 ngOnInit 中订阅 checkProjectManagePermission()。

To ensure you don't get a memory leak, assign checkProjectManagePermission() to a variable, and then call unsubscribe on it inside ngOnDestroy.为确保您不会出现内存泄漏,请将 checkProjectManagePermission() 分配给一个变量,然后在 ngOnDestroy 中调用取消订阅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM