<view class="form"> <input type="text" name='uname' model:value="{{user.uname}}" /> <button bind:tap="submitModel">submit</button> </view>
data: { user:{ uname:'gl' } }
submitModel() { console.log(this.data.user); }
data: { msg: 'hi', obj:{ id: '20201001' name:'hi,there.' } }
普通方式实现双向绑定单一属性 msg - 利用事件bindinput修改数据
<view>{{msg}}</view> <input type="text" value='{{msg}}' bindinput="ipt"/>
ipt(e) { console.log(e.detail.value); this.setData({ msg:e.detail.value }) }
model方式实现双向绑定 - 简单粗暴;省略了bindinput事件
<input type="text" model:value='{{msg}}' />
双向绑定复合数据 obj.name
<input type="text" model:value='{{obj.name}}' />
失败:仅实现逻辑层到视图层的绑定;Two-way binding does not support complex data paths currently. This two-way binding is ignored.
调整:还原为普通事件流bindchange
<input type="text" value='{{obj.name}}' bindinput="getName"/>
getName(e) { this.setData({ 'obj.name':e.detail.value }) }
绑定的是当前swiper-item - current
拖动或设置为自动播放,查看绑定情况
cur:1
<view>{{cur}}</view> <swiper model:current="{{cur}}"> <swiper-item> 11 </swiper-item> <swiper-item> 22 </swiper-item> <swiper-item> 33 </swiper-item> <swiper-item> 44 </swiper-item> </swiper>
cur: 1
<scroll-view class="wrap" scroll-x scroll-with-animation model:scroll-into-view="id{{cur}}"> <view class="item" wx:for="{{10}}" wx:key="index" id="id{{index}}">{{item}}</view> </scroll-view> <swiper class="swiper-wrap" model:current="{{cur}}"> <swiper-item class="swiper-item" wx:for="{{10}}" wx:key="index"> {{item}} </swiper-item> </swiper>
<radio-group name="ugender"> <radio value="male" color="#ff4400" model:checked="{{user.ugender=='male'}}" />male <radio value="female" color="#ff4400" model:checked="{{user.ugender=='female'}}"/>female </radio-group>