在res.StatusDescription Typescript中查找元素

时间:2018-02-20 09:19:40

标签: angular typescript

在这部分中,我可以展示角色的许可。喜欢这个

 let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    return this.http.post(Api.getUrl(Api.URLS.Login), body, {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
        let permissions: any[] = res.StatusDescription.Permisions;
           console.log(permissions)
        if (res.StatusCode === 0 && res.Token) {
          this.currentUser = {
            username: username,
            token: res.Token
          }
          localStorage.setItem(AuthService.CURRENT_USER, JSON.stringify(this.currentUser));
          return true;
        } else {
          return false;
        }
      }
      );

在控制台中显示此数组。照片 enter image description here

现在我想创建在这个数组上找到元素的函数!你能建议我,如何实施?

1 个答案:

答案 0 :(得分:1)

<强> auths.component.ts

export class AuthsComponent {

permissions:string[] = [];

findPermission(permission:string){
    return this.permissions.find(p => p===permission)
}

// this is your function with some modification
call(){
let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    return this.http.post(Api.getUrl(Api.URLS.Login), body, {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
        // HERE : set the permissions value
        this.permissions = res.StatusDescription.Permisions;
           console.log(permissions)
        if (res.StatusCode === 0 && res.Token) {
          this.currentUser = {
            username: username,
            token: res.Token
          }
          localStorage.setItem(AuthService.CURRENT_USER, JSON.stringify(this.currentUser));
          return true;
        } else {
          return false;
        }
      }
      );
    }

    }

<强> auths.component.html

<span *ngIf="findPermission('app_usersgetall')"></span>