鍍金池/ 問答/HTML/ 關(guān)于wx:for 動(dòng)態(tài)循環(huán)一個(gè)數(shù)組的情況

關(guān)于wx:for 動(dòng)態(tài)循環(huán)一個(gè)數(shù)組的情況

項(xiàng)目需要是進(jìn)入微信小程序,在onLoad 函數(shù)中使用wx.request 向服務(wù)器發(fā)起請(qǐng)求,然后將請(qǐng)求通過this.setData 保存到一個(gè)data:{items:[]}屬性中,在頁面中使用<block wx:for="{{itmes}}">{{item.name}}</block> 卻沒有數(shù)據(jù),這是為什么呢?

// index.wxml
<block wx:for="{{itmes}}">
    <view class='item'>
          <text>{{item.name}}</text>
          <text>{{item.desc}}</text>
     </view>
</block>
// index.js
    data:{
      items:[]
    },
   onLoad: function () {
    var that = this;
    wx.request({
      url: 'http://localhost:8888/test.php',
      success: (res) => {
        console.log(res)
        // 可以正確輸出數(shù)組數(shù)據(jù)
   
        that.setData({
          items: res
        })
        console.log(that.data.activity)
        // 也可以正確輸出數(shù)據(jù)
      }
    })

實(shí)在是沒有想通是怎么回事,我以為是異步的問題,所以使用點(diǎn)擊按鈕發(fā)起請(qǐng)求的方式試驗(yàn)了一次,結(jié)果相同.

回答
編輯回答
只愛你

clipboard.png

2018年7月21日 17:15
編輯回答
掛念你

是res.data么...先打印下回調(diào)函數(shù)里有沒有接受到數(shù)據(jù)啊。。。

2017年4月28日 19:31
編輯回答
朽鹿
that.setData({
          activity: res
        })

你把數(shù)據(jù)存在data.activity中,渲染頁面的是data.items 怎么會(huì)有數(shù)據(jù)呢?

2018年3月14日 20:31