微信小程序 图片加载失败处理方法

微信小程序 官方文档对image 媒体组件加载失败 没有太多的解释,使用中出现了几个小问题,今天抽空记录一下

WXML:
<image class="userinfo-avatar" src="{{avatar}}" binderror="errorFunction"></image>

JS:

errorFunction: function(){
    this.setData({
      avatar: \'/image/head.png\'
    })    
}

WXML:

<view wx:for="{{branchList}}" wx:key="{{key}}" wx:for-index=\'index\'>
      <image src=\'{{item.branch_photo}}\' binderror="errorFunction" data-index=\'{{index}}\'></image>
</view>
errorFunction: function (event) {
   console.log(event)
   var index = event.currentTarget.dataset.index
   var img = \'branchList[\'+index+\'].branch_photo\'
     this.setData({
       [img]: \'/pages/resource/img/loading.gif\'
     })   
 }