[英]'Undefined' in Angular 2 Observables and Http
我发现在角度2中将可观察变量与http一起使用确实很困难。 在下面的代码中,我成功获取了in method:
在控制台中成功打印了value。 但是Main:
在我尝试打印时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;
}
}
您在map
块中缺少return
语句。
ngAfterViewInit() {
var keyups = this.searchBox
.valueChanges
.debounceTime(200)
.map(searchTerm => {
return this.getResults(searchTerm);
});
var subscription = keyups.subscribe(res => console.log("Main:" + res));
}
您不会从map
函数返回任何内容。 应该是
.map(searchTerm => {
return this.getResults(searchTerm);
}
要么
.map(searchTerm => this.getResults(searchTerm))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.