angular – forEach不是Ionic 2的功能

前端之家收集整理的这篇文章主要介绍了angular – forEach不是Ionic 2的功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
即使this.toilets充满了对象,我也收到了这个错误

enter image description here

日志首先显示this.location,然后是this.toilets,其中包含所显示的对象.

this.loadHomeData().then(data => {
                this.toilets = data;
                console.log(this.toilets);

            });
loadHomeData() {
        return new Promise(resolve => {
            this.http.get('http://url-to-my-server/' + this.location.coords.latitude + '/' + this.location.coords.longitude)
                .map(res => res.json())
                .subscribe(data => {
                    this.data = data;
                    resolve(this.data);
                });
        });
    }
this.toilets.forEach((toilet) => {
                console.log(toilet);
           });

解决方法

forEach正在使用数组而不是对象.

您需要获取密钥然后迭代

Object.keys(this.toilets).forEach(key=> {
    console.log(this.toilets[key])  ;     
});

猜你在找的Angularjs相关文章