각도 2의 http와 함께 관측 값을 사용하는 것이 정말 어렵습니다. 아래 코드에서 in method:
값이 콘솔에 성공적으로 인쇄됩니다. 그러나 인쇄를 시도했을 때 Main:
은 undefined
입니다.Angular 2 Observables and Http에서 'Undefined'
Main
에서 값을 얻으려면 어떻게해야합니까?
템플릿 코드 :
template:`
<input
class="form-control"
[formControl]="searchBox"
placeholder="Search Spotify artist" />
`
구성 요소 코드 :
export class SpotifySearchComponent {
private searchBox = new FormControl();
constructor(private _http: Http){
}
ngAfterViewInit() {
var keyups = this.searchBox
.valueChanges
.debounceTime(200)
.map(searchTerm => {
this.getResults(searchTerm);
});
var subscription = keyups.subscribe(res => console.log("Main:" + res));
}
getResults(searchTerm){
console.log("in method:" + searchTerm);
return searchTerm;
}
}