微信小程序雙層循環獲取外層索引值

小程序裏有時候頁面會有兩層循環,第二層循環裏怎麼取到父級的index值呢,代碼如下:

    <view wx:for="{{data}}" wx:key="index"  class="albumConItem">
      <view  class="title">{{item.mainAlbumName}}</view>
      <view class="sty" >
        <image  wx:for="{{item.photo}}" 
                wx:for-index="childindex" 
                wx:for-item="items"  
                data-parent='{{index}}'
                data-imgindex="{{childindex}}"
                binderror="imgError"
                src="{{items.imageName==null || items.imageName ==''?'/weixin/common/default_house_list_404.png': items.imageName}}" ></image> 
      </view>
    </view>

第一層循環  wx:for="{{data}}" wx:key="index"   索引值是index

第二層循環  wx:for="{{item.photo}}" wx:for-index="childindex"   索引值是childindex,在第二層就可以直接取第一層的index了

 

我這裏是圖片onerror會用到父級的索引值  所以 在imgError方法裏就可以直接打印到 兩個下標

imgError:function(e){
    console.log('當前下標---',e.target.dataset.imgindex)
    console.log('當前父級下標---',e.target.dataset.parent)
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章