<input type='text' value='{{item.id}}' wx:for="{{objectArray}}" wx:key="unique" style="display: block;"></input>
<button bindtap="switch"> Switch </button>
<button bindtap="addToFront"> Add to the front </button>
<input type='text' value='{{item}}' wx:for="{{numberArray}}" wx:key="*this" style="display: block;"></input>
<button bindtap="addNumberToFront"> Add to the front </button>
const app = getApp()
Page({ data: { objectArray: [ { id: 5, unique: 'unique_5' }, { id: 4, unique: 'unique_4' }, { id: 3, unique: 'unique_3' }, { id: 2, unique: 'unique_2' }, { id: 1, unique: 'unique_1' }, { id: 0, unique: 'unique_0' }, ], numberArray: [1, 2, 3, 4] }, switch: function (e) { const length = this.data.objectArray.length for (let i = 0; i < length; ++i) { const x = Math.floor(Math.random() * length) const y = Math.floor(Math.random() * length) const temp = this.data.objectArray[x] this.data.objectArray[x] = this.data.objectArray[y] this.data.objectArray[y] = temp } this.setData({ objectArray: this.data.objectArray }) }, addToFront: function (e) { const length = this.data.objectArray.length this.data.objectArray = [{ id: length, unique: 'unique_' + length }].concat(this.data.objectArray) this.setData({ objectArray: this.data.objectArray }) }, addNumberToFront: function (e) { this.data.numberArray = [this.data.numberArray.length + 1].concat(this.data.numberArray) this.setData({ numberArray: this.data.numberArray }) } })
对。或者你可以选择不要设置input那一项数据。
你好,是不是使用setData时包含旧数据,导致input值被还原了?
根本原因是不是因为小程序是单向数据绑定,如果ui界面即这里input的值发生改变,要手动对data里面的值进行更新,那么数据才能同步?