Data binding in WeChat applet tutorial

Source: Internet
Author: User
In the mini-program tutorial, the dynamic data in WXML comes from the data of the corresponding Page. Data Binding

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

Simple binding

Data binding uses the "Mustache" syntax (double braces) to package variables, which can act on:

Content

{Message }}

Page({   data: {   message: 'Hello MINA!'   }  })


Component attributes (must be within double quotation marks)

Page({   data: {   id: 0   }  })

Control attributes (must be within double quotation marks)

Page({   data: {   condition: true   }  })

Operation

You can perform simple operations within {}. the following methods are supported:

Ternary computation

Hidden

Arithmetic operation

{A + B }}+ {c} + d

Page({   data: {   a: 1,   b: 2,   c: 3   }

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

Logical judgment



String operation

{"Hello" + name }}

Page({   data:{   name:"MINA"   }  })

Combination

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

Array

{Item }}

Page({   data: {   zero: 0   }  })

Finally, it is combined into an array [0, 1, 2, 3, 4].

Object

   Page({   data: {   a: 1,   b: 2   }  })

The final combination objects are {for: 1, bar: 2}

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

   Page({   data: {   obj1: {    a: 1,    b: 2   },   obj2: {    c: 3,    d: 4   }   }  })

The final combination objects are {a: 1, B: 2, c: 3, d: 4, e: 5}

If the key and value of an object are the same, it can be indirectly expressed

Page({   data: {   foo: 'my-foo',   bar: 'my-bar'   }  })

The final combination object is {foo: 'My-Foo', bar: 'My-bar '}

Note: The preceding method can be combined at will, but if the variable name is the same, the latter will overwrite the former, as shown in figure

Page({   data: {   obj1: {    a: 1,    b: 2   },   obj2: {    b: 3,    c: 4   },   a: 5   }  })

The final combination objects are {a: 5, B: 3, c: 6}

The above is the data binding content of the mini-program tutorial. For more information, see The PHP Chinese website (www.php.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.