Home Web Front-end JS Tutorial AngularJS radio button and multi-select box realize two-way dynamic binding_AngularJS

AngularJS radio button and multi-select box realize two-way dynamic binding_AngularJS

May 16, 2016 pm 03:20 PM
angularjs dynamic binding Single box Checkbox

When it comes to two-way data binding in AngularJS, everyone will definitely think of the ng-model directive.

1. ng-model

The

ng-model directive is used to bind input, select, textarea or custom form controls to properties in the scope that contains them. It binds the value of the operation expression in the current scope to the given element. If the property does not exist, it is implicitly created and added to the current scope.
Always use ng-model to bind properties within a data model on the scope instead of properties on the scope. This can avoid property overrides in the scope or descendant scopes!

<input type="text" ng-model="modelName.somePrototype" />
Copy after login

2. type=”radio”

Specify the corresponding value in the selected state through the value attribute, and use ng-model to map the radio button to the attribute in $scope, thereby realizing two-way dynamic binding when type="radio".

<input type="radio" name="sex" value="male" ng-model="person.sex" />男
<input type="radio" name="sex" value="female" ng-model="person.sex" />女

Copy after login

3. type="checkbox"

Use AngularJS’s built-in instructions ng-true-value and ng-false-value to specify the corresponding values ​​of the multi-select box in the selected and unselected states, and then use ng-model to correspond to the attributes in $scope. This realizes the two-way dynamic binding of type="checkbox".

<input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.pingpong" />乒乓球
<input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.football" />足球
<input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.basketball" />篮球

Copy after login

4. Complete example

<html ng-app="myApp">
<head>
  <meta charset="UTF-8">
  <title>radio & checkbox</title>
  <script type="text/javascript" src="angular.js/1.4.4/angular.min.js"></script>
</head>
<body>
  <input type="radio" name="sex" value="male" ng-model="person.sex" />男
  <input type="radio" name="sex" value="female" ng-model="person.sex" />女
  <input type="text" ng-model="person.sex" />

  <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.pingpong" />乒乓球
  <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.football" />足球
  <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.basketball" />篮球
  <span>{{ person.like.pingpong }} {{ person.like.football }} {{ person.like.basketball }} </span>
</body>
</html>

<script type="text/javascript">
  var app = angular.module('myApp', []);
  app.run(function($rootScope) {
    $rootScope.person = {
      sex: "female",
      like: {
        pingpong: true,
        football: true,
        basketball: false
      }
    };
  });
</script>
Copy after login

The above is the relevant introduction to the two-way dynamic binding of AngularJS radio button and multi-select box. I hope it will be helpful to everyone's learning.

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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 dynamically bind and update form data in Vue How to dynamically bind and update form data in Vue Oct 15, 2023 pm 02:24 PM

How to dynamically bind and update form data in Vue With the continuous development of front-end development, forms are an interactive element that we often use. In Vue, dynamic binding and updating of forms is a common requirement. This article will introduce how to dynamically bind and update form data in Vue, and provide specific code examples. 1. Dynamic binding of form data Vue provides the v-model instruction to achieve two-way binding of form data. Through the v-model directive, we can compare the value of the form element with the Vue instance

The relationship between multi-select box check and select all function in Vue document The relationship between multi-select box check and select all function in Vue document Jun 21, 2023 am 09:33 AM

With the continuous development and advancement of front-end technology, the application of the Vue framework is becoming more and more popular. In practical applications of Vue, the functions of checking multiple boxes and selecting all are often used. In the Vue documentation, the implementation methods of checking multiple selection boxes and selecting all are also very detailed. Multi-select box in Vue In Vue, the implementation method of multi-select box is very simple. You only need to use &lt;inputtype="checkbox"&gt; where you need to use multi-select box. When multiple selections need to be made

How to use Vue to implement multi-select boxes and radio buttons How to use Vue to implement multi-select boxes and radio buttons Nov 07, 2023 am 11:42 AM

How to use Vue to implement multi-select boxes and radio buttons Vue is a popular JavaScript framework that is widely used in web development. In Vue, we can easily implement various interactive effects, including multi-select boxes and radio button boxes. This article will introduce how to use Vue to implement multi-select boxes and radio buttons, and provide specific code examples. Implementing a multi-select box A multi-select box is used to allow the user to select multiple options. In Vue, we can use the v-model directive to implement two-way data binding of multi-select boxes. Here is a simple example

The latest 5 angularjs tutorials in 2022, from entry to mastery The latest 5 angularjs tutorials in 2022, from entry to mastery Jun 15, 2017 pm 05:50 PM

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Jun 27, 2023 pm 07:37 PM

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

Build web applications using PHP and AngularJS Build web applications using PHP and AngularJS May 27, 2023 pm 08:10 PM

With the continuous development of the Internet, Web applications have become an important part of enterprise information construction and a necessary means of modernization work. In order to make web applications easy to develop, maintain and expand, developers need to choose a technical framework and programming language that suits their development needs. PHP and AngularJS are two very popular web development technologies. They are server-side and client-side solutions respectively. Their combined use can greatly improve the development efficiency and user experience of web applications. Advantages of PHPPHP

Solution to the multi-select box all-select function in Vue development Solution to the multi-select box all-select function in Vue development Jun 30, 2023 pm 02:00 PM

How to solve the problem of the select-all function of multi-select boxes in Vue development. In Vue development, we often encounter scenarios where we need to use multi-select boxes for batch operations, and sometimes we also need to implement a select-all function, that is, select all and multiple-select. When the box is selected, all sub-options must also be selected. This article will introduce how to use Vue to solve the problem of selecting all functions of multi-select boxes. Preparation work First, in Vue development, we need to use Vue's calculated properties and event binding. In the Vue component, define a data attribute to store the selection of the multi-select box.

Build a single-page web application using Flask and AngularJS Build a single-page web application using Flask and AngularJS Jun 17, 2023 am 08:49 AM

With the rapid development of Web technology, Single Page Web Application (SinglePage Application, SPA) has become an increasingly popular Web application model. Compared with traditional multi-page web applications, the biggest advantage of SPA is that the user experience is smoother, and the computing pressure on the server is also greatly reduced. In this article, we will introduce how to build a simple SPA using Flask and AngularJS. Flask is a lightweight Py

See all articles