# Data binding
The dynamic data in WXML comes from the data corresponding to the Page.
# Simple binding
Data binding wraps variables using Mustache syntax (double braces) and can act on:
# content
<view> {{ message }} </view>
Page({
data: {
message: 'Hello MINA!'
}
})
# Component properties (required in double quotation marks)
<view id="item-{{id}}"> </view>
Page({
data: {
id: 0
}
})
# Control properties (required in double quotes)
<view wx:if="{{condition}}"> </view>
Page({
data: {
condition: true
}
})
# Keywords (required in double quotation marks)
True: Boolean type true, representing the truth value.
False: Boolean type false, representing the false value.
<checkbox checked="{{false}}"> </checkbox>
Special note: Don't writechecked = "false",The result is a character string that is converted to a boolean to represent the true value.
# operation
Simple operations can be performed within{{}}in the following ways:
# ternary operator
<view hidden="{{flag ? true : false}}"> Hidden </view>
# Arithmetic operations
<view> {{a + b}} + {{c}} + d </view>
Page({
data: {
a: 1,
b: 2,
c: 3
}
})
The content in view is3 + 3 + d.
# Logical judgment
<view wx:if="{{length > 5}}"> </view>
# Character string operation
<view>{{"hello" + name}}</view>
Page({
data:{
name: 'MINA'
}
})
# Data Path Computing
<view>{{object.key}} {{array[0]}}</view>
Page({
data: {
object: {
key: 'Hello '
},
array: ['MINA']
}
})
# combination
You can also directly combine in Mustache to form a new object or array.
# array
<view wx:for="{{[zero, 1, 2, 3, 4]}}"> {{item}} </view>
Page({
data: {
zero: 0
}
})
Finally combined into an array[0, 1, 2, 3, 4].
# object
<template is="objectCombine" data="{{foo: a, bar: b}}"></template>
Page({
data: {
a: 1,
b: 2
}
})
The final object is{foo: 1, bar: 2}
You can also use the extension operator...to expand an object
<template is="objectCombine" data="{{...obj1, ...obj2, e: 5}}"></template>
Page({
data: {
obj1: {
a: 1,
b: 2
},
obj2: {
c: 3,
d: 4
}
}
})
The final composition is{a: 1, b: 2, c: 3, d: 4, e: 5}.
If the key and value of the object are the same, it can also be expressed indirectly.
<template is="objectCombine" data="{{foo, bar}}"></template>
Page({
data: {
foo: 'my-foo',
bar: 'my-bar'
}
})
The final object is{foo: 'my-foo', bar: 'my-bar'}.
Note : The above methods can be combined arbitrarily, but if there is a case of the same variable name, the latter will override the former, such as:
<template is="objectCombine" data="{{...obj1, ...obj2, a, c: 6}}"></template>
Page({
data: {
obj1: {
a: 1,
b: 2
},
obj2: {
b: 3,
c: 4
},
a: 5
}
})
The final combination is{a: 5, b: 3, c: 6}.
Note: A space between curly braces and quotes will eventually resolve to a character string
<view wx:for="{{[1,2,3]}} ">
{{item}}
</view>
It's equivalent to
<view wx:for="{{[1,2,3] + ' '}}">
{{item}}
</view>