【发布时间】:2018-01-07 07:22:34
【问题描述】:
我有一个服务,它使用 GET 请求从 rest 调用(firebase)获取 json 数据。我正在尝试在 middle-box.component.html 上显示响应数据
修复此错误的任何建议或解决方案:
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
twitter.service.ts
@Injectable()
export class TwitterService {
constructor(private http: Http) {}
getJobs() {
var jobs = {}
console.log('Twitter Service Called !');
return this.http.get('https://dmm-db.firebaseio.com/jobs.json')
.map(
(response: Response) => {
var data = response.json();
var jobs = [], item;
for (var type in data) {
console.log(type);
item = {};
item.name = type;
jobs.push(item);
}
console.log(jobs);
return jobs;
}
)
}
}
middle-box.component.ts
@Component({
selector: 'app-middle-box',
templateUrl: './middle-box.component.html',
styleUrls: ['./middle-box.component.css']
})
export class MiddleBoxComponent implements OnInit {
constructor(private twitterService: TwitterService) {
twitterService.getJobs()
.subscribe(
(jobs: any[]) => this.jobs = jobs,
(error) => console.log('Error: '+ error),
() => console.log('Completed')
);
}
middle-box.component.html
<ul class="list-group" *ngFor="let job of jobs">
<li class="list-group-item">{{ job.name }}</li>
</ul>
【问题讨论】:
-
顺便说一句,我已经阅读了其他解决方案并尝试了一些东西,但都没有奏效。而且我是角度的新手,所以如果我在这里缺少基本构造,请原谅我。但我确实需要帮助,谢谢:)
标签: angular