微信小程序-for 指令


使用for指令可以實現循環

使用wx:for標簽

index和item默認表示索引和value

wxml:

<text>商品列表信息</text>
<view wx:for="{{dataList}}">
索引:{{index}} -  內容:{{item}}
</view>

<text>商品列表信息-自定義變量</text>
<view wx:for="{{dataList}}" wx:for-index="ind" wx:for-item='value'>
{{ind}} -  {{value}}
</view>

<text>用戶字典信息</text>
<view>
{{userInfo.name}} - {{userInfo.age}}
</view>

<view wx:for="{{userInfo}}">
字典key:{{index}} - 對應value:{{item}}
</view>

<text>列表字典信息</text>
<view wx:for-items="{{dataInfo}}" >
{{item.name}} -  {{item.num}}斤-{{item.price}}元
</view>

js:

Page({

  /**
   * 頁面的初始數據
   */
  data: {
    dataList: ['白菜', '蘿卜', '生菜'],
    userInfo: {
      'name': 'xiaoming',
      'sex': 'male',
      'age': 18
    },
    dataInfo: [{
      'name': '白菜',
      'num': 5,
      'price': 50
    }, {
      'name': '蘿卜',
      'num': 10,
      'price': 30
    }],
    imageList: ["/images/default.png", "/images/hg.jpg"]
  },

 

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM