微信小程序開發tips
阿新 • • 發佈:2018-05-20
tro 綁定 跳轉頁面 div dataset 程序 options 文件 tar
1、wx:for渲染後傳值問題:
用wx:for渲染後,如果想在綁定的事件中獲取點擊項中的數據,則在wx:for的標簽的屬性中要加入data-item屬性,具體如下:
a.wml文件
<view wx:for="{{MeetingRoom}}" bindtap=‘fordetails‘ data-name="{{item.name}}"> <view > <view > <view >{{item.name}}</view> <view >地址:{{item.location}}</view> </view> </view>
a.js文件
Page({ data: { MeetingRoom:[{ id:"1", name:"1號會議室", location:"1樓101室" },{ id: "2", name: "2號會議室", location: "2樓202室" },{ id: "3", name: "3號會議室", location: "3樓303室" },{ id: "4", name: "4號會議室", location: "4樓404室" }, { id: "5", name: "5號會議室", location: "5樓505室" }] }, fordetails:function(e){ console.log(e.currentTarget.dataset.name) wx.navigateTo({ url: ‘../detail/detail‘, }) })
data-item中設置的key對應的value就可以在e.currentTarget.dataset中獲取到
2、跨頁面傳值
若想進行跨頁面傳值,則需要在頁面跳轉語句的地址中追加需要傳的值,代碼如下:
1 wx.navigateTo({ 2 url: ‘../detail/detail?name=1號會議室‘ 3 })
取值可以在跳轉頁面的onLoad函數中加載,代碼如下:
1 onLoad: function (options) { 2 this.setData({ 3 roomname:options.name 4 }) 5 }
微信小程序開發tips