# Data Binding

Dynamic data in FXML comes from the corresponding Page's data.

# 1. Simple Binding

Data binding uses double curly braces to wrap variables and can be applied to:

# 1.1 Content

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

# 1.2 Component Attributes (must be inside double quotes)

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

# 1.3 Control Attributes (must be inside double quotes)

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

# 1.4 Keywords (must be inside double quotes)

true: boolean type true, representing a truthy value.

false: boolean type false, representing a falsy value.

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

Note

Do not write checked="false" directly, as its evaluation result is a string, which converts to a truthy value when cast to boolean type.

# 2. Operations

Simple operations can be performed within {{}}, supporting the following methods:

# 2.1 Ternary Operation

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

# 2.2 Arithmetic Operations

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

The content in the view is 3 + 3 + d.

# 2.3 Logical Judgment

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

# 2.4 String Operations

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

# 2.5 Data Path Operations

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

# 3. Combination

You can also directly combine within Mustache to form new objects or arrays.

# 3.1 Array

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

The final combined array is [0, 1, 2, 3, 4].

# 3.2 Object

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

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

You can also use the spread 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 combined object is {a: 1, b: 2, c: 3, d: 4, e: 5}.

If the object's key and value 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 combined object is {foo: 'my-foo', bar:'my-bar'}.

Note

The above methods can be combined arbitrarily, but if there are variables with the same name, the latter will overwrite the former, for example:

<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 combined object is {a: 5, b: 3, c: 6}.

Note

If there is a space between the curly braces and quotes, it will ultimately be parsed as a string

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

Equivalent to

<view wx:for="{{[1,2,3] + ' '}}">
  {{item}}
</view>
© FinClip with ❤ , Since 2017