鍍金池/ 問答/HTML5  HTML/ 通過promise獲取數(shù)據(jù)改變data后,使用wx:for循環(huán)不渲染頁面

通過promise獲取數(shù)據(jù)改變data后,使用wx:for循環(huán)不渲染頁面

在onload里面通過promise異步獲取三個(gè)城市的天氣情況,改變data里面的數(shù)據(jù)。view使用wx:for循環(huán),渲染出來。但是AppData里面存在著數(shù)據(jù),卻渲染不出來,是異步的原因么

//獲取緩存的城市名稱
 wx.getStorage({
            key: 'default',
            success: function(res) {
                var arr = [];
                // 循環(huán)請(qǐng)求數(shù)據(jù)
                for (let i = 0; i < res.data.length; i++) {
                    wx.request({
                        url: 'https://free-api.heweather.com/s6/weather', //僅為示例,并非真實(shí)的接口地址
                        data: {
                            location: res.data[i].city,
                            key: "4f4429b5eb4e4aee9343ad8429909de3"
                        },
                        header: {
                            'content-type': 'application/json' // 默認(rèn)值
                        },
                        success: function(params) {
                            //添加到data里
                            arr.push(params.data.HeWeather6[0]);
                            _this.setData({
                                forecast: arr
                            })
                        }
                    })
                }
            }
        })

在appdata里面也是有數(shù)據(jù)存在的
圖片描述

回答
編輯回答
有點(diǎn)壞

你最好先在view的template里用{{ forecast }}看看數(shù)據(jù)是否可以顯示出來,如果可以,那就是模板和數(shù)據(jù)格式不匹配造成的。否則就是數(shù)據(jù)無法被view訪問到。

2017年11月9日 08:46