Skip to content

Data Binding

The dynamic data in WXML all comes from the data of the corresponding Page.

Simple binding

Data binding uses Mustache syntax (double curly braces) to wrap variables and can be used for:

Content

js
<view> {{ message }} </view>
js
Page({
  data: {
    message: 'Hello MINA!'
  }
})

Component attributes (need to be in double quotes)

js
<view id="item-{{id}}"> </view>
js
Page({
  data: {
    id: 0
  }
})

Control attributes (need to be in double quotes)

js
<view wx:if="{{condition}}"> </view>
js
Page({
  data: {
    condition: true
  }
})

Keywords (need to be in double quotes)

true:True of boolean type represents true value.

false: False of boolean type represents false value.

js
<checkbox checked="{{false}}"> </checkbox>

TIP

Do not write checked='false' directly, the result of calculation is a string, which represents true value after conversion to boolean type.

Operation

You can perform simple operations within the curly brackets. The following methods are supported:

Ternary operation:

js
<view hidden="{{flag ? true : false}}"> Hidden </view>

Arithmetic operation:

js
<view> {{a + b}} + {{c}} + d </view>
js
Page({
  data: {
    a: 1,
    b: 2,
    c: 3
  }
})

The content in view is 3 + 3 + d.

Logic judgment

js
<view wx:if="{{length > 5}}"> </view>

String operation

js
<view>{{"hello" + name}}</view>
js
Page({
  data:{
    name: 'MINA'
  }
})

Data path operation

js
<view>{{object.key}} {{array[0]}}</view>
js
Page({
  data: {
    object: {
      key: 'Hello '
    },
    array: ['MINA']
  }
})

Combination

You can also combine directly in Mustache to form a new object or array.

Array

js
<view wx:for="{{[zero, 1, 2, 3, 4]}}"> {{item}} </view>
js
Page({
  data: {
    zero: 0
  }
})

The final combined object is {a: 1, b: 2, c: 3, d: 4, e: 5}.

Object

js
<template is="objectCombine" data="{{for: a, bar: b}}"></template>
js
Page({
  data: {
    a: 1,
    b: 2
  }
})

The final combined object is {for: 1, bar: 2}

You can also use the extension operator... to expand an object

js
<template is="objectCombine" data="{{...obj1, ...obj2, e: 5}}"></template>
js
Page({
  data: {
    obj1: {
      a: 1,
      b: 2
    },
    obj2: {
      c: 3,
      d: 4
    }
  }
})

The final combined object is {a: 1, b: 2, c: 3, d: 4, e: 5}.

If the key and value of an object are the same, they can also be expressed indirectly.

js
<template is="objectCombine" data="{{foo, bar}}"></template>
js
Page({
  data: {
    foo: 'my-foo',
    bar: 'my-bar'
  }
})

The resulting object is {foo: 'my-foo', bar: 'my-bar'}.

The above methods can be combined at will, but if there are identical variable names, the latter will overwrite the former, such as:

js
<template is="objectCombine" data="{{...obj1, ...obj2, a, c: 6}}"></template>
js
Page({
  data: {
    obj1: {
      a: 1,
      b: 2
    },
    obj2: {
      b: 3,
      c: 4
    },
    a: 5
  }
})

The final combined object is {a: 5, b: 3, c: 6}.

TIP

If there is a space between the curly braces and the quotation marks, it will eventually be parsed into a string.

js
<view wx:for="{{[1,2,3]}} ">
  {{item}}
</view>

This is equivalent to

js
<view wx:for="{{[1,2,3] + ' '}}">
  {{item}}
</view>

TIP

If there is a space between the curly braces and the quotation marks, it will eventually be parsed into a string.

js
<view wx:for="{{[1,2,3]}} ">
  {{item}}
</view>

This is equivalent to

js
<view wx:for="{{[1,2,3] + ' '}}" >
  {{item}}
</view>