2017-11-21 13 views
0

내 컴퓨터에서 로컬로 호스팅되는 json 파일을 쿼리하는 서비스가 있습니다. 서비스에 대한 쿼리가 호출되었지만 서비스 쿼리에서 구성 요소로 json 데이터를 반환하면 항상 비어 있습니다. 그러나 브라우저에서 개발자 도구를 사용하면 데이터가있는 JSON 파일이 실제로 200 OK로 반환된다는 것을 알 수 있습니다.각도 2 -이 서비스가 데이터를 반환하지 않는 이유

돌아 오는 관찰 가능 데이터에 내 구독에 근본적으로 잘못된 점이 있습니까? 어떤 도움이라도 대단히 감사합니다. 당신의 userService.search 관찰과

@Injectable() 
export class UserService { 

    constructor(
     public http: Http, 
    ) {} 

    search(query: string): Observable<UserInfo[]> { 
     return this.http.get('http://localhost/test.json') 
     .map(res => res.json()); 


    } 
} 




valuechange(query) { 

    console.log("calling service from component"); 

    this.inputFormControl.valueChanges 
    .debounceTime(4000) 
    .distinctUntilChanged() 
    .subscribe(query => this.userService.search(query) 
    .subscribe(data => { 
     { 
      this.data = data 
      console.log('this.users=' + this.data); 
      console.log('this.users.length=' + this.data.length); 
     } 

     })); 
    console.log("data on return from service is " + this.data[1]); 
} 
+0

http : //localhost/test.json '을 웹 사이트의 상대 경로로 바꾸어 보았습니까? – Moutabreath

+2

이 줄은 콘솔에 무엇을 기록합니까? console.log ('this.users ='+ this.data); – Notmfb

답변

1

당신은 체인에 관찰 순서에 valueChanges 이벤트 이미 터를 switchMap 같은 것을 사용한다.

this.inputFormControl.valueChanges 
.debounceTime(4000) 
.distinctUntilChanged() 
.switchMap(query => this.userService.search(query)) 
.subscribe(data => { 
    this.data = data 
    console.log('this.users=' + this.data); 
    console.log('this.users.length=' + this.data.length); 
});