Home WeChat Applet Mini Program Development Example analysis of data binding in WeChat applet (code)

Example analysis of data binding in WeChat applet (code)

Aug 21, 2018 pm 04:38 PM
data binding

This article brings you an example analysis (code) of data binding in WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. The WeChat applet cannot bind the tags in the wxml page from the js page to obtain or set the value or attribute of the tag. All are implemented using data binding
2. The dynamic data in WXML comes from the data of the corresponding Page.

Data binding:
1. Simple data binding

wxml中应用双大括号将data中的数据绑定到相应的标签中:
<view> {{ message }} </view>
js中:
Page({
  data: {
    message: 'Hello MINA!'
  }
})
Copy after login

2. Binding of label attributes

wxml中,其中绑定的要在双引号之中:
<view id="{{id}}"> </view>
js中:
Page({
  data: {
    id: 0
  }
})
Copy after login

3. Control Attribute binding

wxml中(绑定在双引号中)
<view wx:if="{{condition}}"> </view>
//作为条件句出现,可以动态决定某一个标签出现不出现
js中:
Page({
  data: {
    condition: true
  }
})
---或---
wxml中(绑定在双引号中)
<view hidden="{{condition}}"> </view>
//作为条件句出现,可以动态决定某一个标签出现不出现
js中:
Page({
  data: {
    condition: true
  }
})
//hidden与wx:if的区别:
hidden只是隐藏,但是节点是生成的
wx:if不生成节点
Copy after login

4. Keywords (need to be within double quotes)

true:boolean 类型的 true,代表真值。

false: boolean 类型的 false,代表假值。

<checkbox checked="{{false}}"> </checkbox>
特别注意:不要直接写 checked="false",其计算结果是一个字符串,转成 boolean 类型后代表真值。
//在新版的微信小程序开发工具中,这个问题好像被解决了,即checked="true"也是对的
Copy after login

5. Operation

1>三元运算

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

2>算数运算

<view> {{a + b}} + {{c}} + d </view>
Page({
  data: {
    a: 1,
    b: 2,
    c: 3
  }
})
view中的内容为 3 + 3 + d。

3>逻辑判断

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

4>字符串运算

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

5>数据路径运算

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

//综上:
所有运算都是在{{}}之中进行的,这时候里面就相当于一些语言代码,而不是要呈现的内容
Copy after login

6. Combination (not very common, no longer If necessary, please refer to the WeChat Mini Program Development Document, Data Binding Section)

Related recommendations:

Customized analysis process of data in WeChat Mini Program

Detailed explanation of the usage of iconfont in WeChat mini program (with code)

WeChat mini program example: implementing random verification code (with code)

The above is the detailed content of Example analysis of data binding in WeChat applet (code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement data binding function in SwiftUI using MySQL How to implement data binding function in SwiftUI using MySQL Jul 30, 2023 pm 12:13 PM

How to implement data binding function in SwiftUI using MySQL

Detailed explanation of data binding functions in Vue documentation Detailed explanation of data binding functions in Vue documentation Jun 20, 2023 pm 10:15 PM

Detailed explanation of data binding functions in Vue documentation

How to use the v-once directive to implement one-time rendering of data binding in Vue How to use the v-once directive to implement one-time rendering of data binding in Vue Jun 11, 2023 pm 01:56 PM

How to use the v-once directive to implement one-time rendering of data binding in Vue

Vue error: v-model cannot be used correctly for two-way data binding. How to solve it? Vue error: v-model cannot be used correctly for two-way data binding. How to solve it? Aug 19, 2023 pm 08:46 PM

Vue error: v-model cannot be used correctly for two-way data binding. How to solve it?

Detailed explanation of v-model function in Vue3: application of two-way data binding Detailed explanation of v-model function in Vue3: application of two-way data binding Jun 18, 2023 am 10:25 AM

Detailed explanation of v-model function in Vue3: application of two-way data binding

How to use Vue for form validation and data binding How to use Vue for form validation and data binding Aug 02, 2023 am 10:54 AM

How to use Vue for form validation and data binding

How does Vue implement two-way binding of data? How does Vue implement two-way binding of data? Jun 27, 2023 pm 04:46 PM

How does Vue implement two-way binding of data?

How to use v-bind to bind data to HTML attributes in Vue How to use v-bind to bind data to HTML attributes in Vue Jun 11, 2023 am 09:11 AM

How to use v-bind to bind data to HTML attributes in Vue

See all articles