我正在按照这篇教程进行学习。在从api.github获取用户列表的过程中,出现了错误:
无法找到支持 '[object Object]' 对象的差异
我认为这与...有关。
<ul>
<li *ngFor = "#user of users">
{{user | json}}
</li>
</ul>
我的代码之前没有任何错误,但我不确定数据是否来自get请求,只是点击没有出现任何错误,以下是我的代码:
@Component({
selector: 'router',
pipes : [],
template: `
<div>
<form [ngFormModel] = "searchform">
<input type = 'text' [ngFormControl]= 'input1'/>
</form>
<button (click) = "getusers()">Submit</button>
</div>
<div>
<ul>
<li *ngFor = "#user of users">
{{user | json}}
</li>
</ul>
</div>
<router-outlet></router-outlet>
`,
directives: [FORM_DIRECTIVES]
})
export class router {
searchform: ControlGroup;
users: Array<Object>[];
input1: AbstractControl;
constructor(public http: Http, fb: FormBuilder) {
this.searchform = fb.group({
'input1': ['']
})
this.input1 = this.searchform.controls['input1']
}
getusers() {
this.http.get(`https://api.github.com/
search/users?q=${this.input1.value}`)
.map(response => response.json())
.subscribe(
data => this.users = data,
error => console.log(error)
)
}
}
bootstrap(router, [HTTP_PROVIDERS])
items
。请参考此文档:https://developer.github.com/v3/search/#search-users 以了解响应有效载荷的格式。这对应于响应有效载荷中的用户数组... 我已更新我的答案。 - Thierry Templier<div #users>
)与我正在为 ngFor 设置数据的变量同名。因此,Angular 用模板变量覆盖了我的变量。facepalm - Isaac Lyman