2016-08-23 158 views
4

我不能將數據存儲到UserDetailsArr對象,PFB代碼供您參考。Angular2服務訂閱不起作用

用戶詳細信息類別:

export class UserDetails 
    { 
     userId:string; 
     userName:string; 
     password:string; 
     firstName:string; 
     lastName:string; 
     mobileNumber:string; 
     email:string 
    } 

服務類:

import {Component} from '@angular/core' 
    import {OnInit} from '@angular/core' 
    import {UserService} from './user.service' 
    import {User} from './user' 
    import {UserDetails} from './user' 
    import {UserDetailComponent} from './user-detail.component' 
    import {HTTP_PROVIDERS} from '@angular/http'; 

    @Component(
     { 
      selector:'user-list', 
      template: ` 
       <br/><br/><br/> 
       <div> 
       <table border='1'> 
       <thead> 
        <tr> 
         <th>First Name </th> 
         <th>Last Name </th> 
         <th>Mobile Number </th> 
         <th>Email </th> 
        </tr> 
       </thead> 
         <tr *ngFor="let user of UserDetailsArr"> 
         <td> <span (click)="showDetails(user)"> {{user.firstName}} </span> </td> 
         <td> {{user.lastName}} </td> 
         <td> {{user.mobileNumber }} </td> 
         <td> {{user.email}} </td> 
        </tr>      
       </table> 
       </div> 
       <user-detail *ngIf = "selectedUser != null" [user] = "selectedUser"></user-detail> 
       <br/><br/><br/> 

      `, 
      providers:[UserService , HTTP_PROVIDERS], 
      directives:[UserDetailComponent] 
     } 
    ) 
    export class UserListComponent implements OnInit 
    { 
     users:User[]; 
     UserDetailsArr: UserDetails[]= []; 
     errorMessage: string = ''; 
     isLoading: boolean = true; 
     public selectedUser = null; 
     constructor(private _userService:UserService) 
     { 

     } 

     ngOnInit():any 
     { 
      this.getUsers(); 
     } 

     getUsers() 
     { 

      this._userService.getUsers() 
        .subscribe(
         /* happy path */ p => this.UserDetailsArr = p, 
         /* error path */ e => this.errorMessage = e, 
         /* onComplete */() => this.isLoading = false);     


      console.log(this.UserDetailsArr); 
     } 

     showDetails(user) 
     { 
      this.selectedUser = user; 
     } 



    } 

Web服務perfecltly工作如果我登錄像下面,

this._userService.getUsers() 
      .subscribe(
       /* happy path */ p => console.log(JSON.stringify(p), 
       /* error path */ e => this.errorMessage = e, 
       /* onComplete */() => this.isLoading = false)); 

用戶數據已經登錄瀏覽器控制檯。

[{"UserId":"bcb444aa-401b-48a7-b1bf-17b4bf78b834","UserName":"prabhu","Password":"pwd","FirstName":"PRABHU","LastName":"ARUMUGAM","MobileNumber":"1234567890","Email":"[email protected]"}, 
    {"UserId":"d9e5ba40-d0d7-4d90-89b0-7e26660cc84b","UserName":"senthil","Password":"pwd","FirstName":"SENTHIL","LastName":"KUMAR","MobileNumber":"1234567890","Email":"[email protected]"}, 
    {"UserId":"a59dabad-5a8c-4ced-9006-2181bf8c10cb","UserName":"vijay","Password":"pwd","FirstName":"VIJAY","LastName":"RAJ","MobileNumber":"1234567890","Email":"[email protected]"}, 
    {"UserId":"f3e2d351-9413-4d80-8623-36556d27f875","UserName":"thamizh","Password":"pwd","FirstName":"THAMIZH","LastName":"VANAN","MobileNumber":"1234567890","Email":"[email protected]"}] 
    (e) { return _this.errorMessage = e; }() { return _this.isLoading = false; } 

但是,如果我的數據分配給UserDetailsArr數據不分配,

 this._userService.getUsers() 
      .subscribe(
       /* happy path */ p => this.UserDetailsArr = p, 
       /* error path */ e => this.errorMessage = e, 
       /* onComplete */() => this.isLoading = false); 
    console.log(this.UserDetailsArr); 

下一行我試圖打印,但它不工作,結果是不確定的。

+1

嗯,當然。您的服務是異步的。這就是爲什麼它返回一個Observable,而不僅僅是一個用戶數組。所以當用戶可用時,很可能當你得到對HTTP請求的響應時,函數'p => this.UserDetailsArr = p'會在console.log(this.UserDetailsArr)行後面執行很長時間得到用戶。 –

回答

0

當數據到達時,可觀察對象稱呼傳遞給subscribe(...)或其他運算符(如)的回調。

之後立即調用subscribe(...)之後的代碼,並且@JbNizet提到這通常在數據到達之前很長時間。

您需要將代碼依賴於從可觀察接收到類似的回調的一個數據:

this._userService.getUsers() 
     .subscribe(
      /* happy path */ p => { 
       this.UserDetailsArr = p; 
       console.log(this.UserDetailsArr); 
      }, 
      /* error path */ e => this.errorMessage = e, 
      /* onComplete */() => this.isLoading = false 
     ); 
+0

感謝它的工作。但是 –

+0

似乎在'but'後面缺少了什麼? –

+0

是的,:-)但我需要通過UserDetailsArr對象循環,並綁定* ngFor指令中的數據,我現在編輯服務類,它不工作我得到空表結果..你可以幫助嗎? –