AngularJS單選框及多選框?qū)崿F(xiàn)雙向動(dòng)態(tài)綁定
在AngularJS中提及雙向數(shù)據(jù)綁定,大家肯定會(huì)想到ng-model指令。
一、ng-model
ng-model指令用來將input、select、textarea或自定義表單控件同包含它們的作用域中的屬性進(jìn)行綁定。它將當(dāng)前作用域中運(yùn)算表達(dá)式的值同給定的元素進(jìn)行綁定。如果屬性不存在,它會(huì)隱式創(chuàng)建并將其添加到當(dāng)前作用域中。
始終用ng-model來綁定scope上一個(gè)數(shù)據(jù)模型內(nèi)的屬性,而不是scope上的屬性,這可以避免在作用域或后代作用域中發(fā)生屬性覆蓋!
<input type="text" ng-model="modelName.somePrototype" />
二、type=”radio”
通過 value 屬性指定選中狀態(tài)下對(duì)應(yīng)的值,并通過 ng-model 將單選框與 $scope 中的屬性對(duì)應(yīng),便實(shí)現(xiàn)了 type=”radio” 時(shí)的雙向動(dòng)態(tài)綁定。
<input type="radio" name="sex" value="male" ng-model="person.sex" />男 <input type="radio" name="sex" value="female" ng-model="person.sex" />女
三、type=”checkbox”
通過AngularJS 的內(nèi)置指令 ng-true-value 和 ng-false-value ,指定多選框在選中和未選中狀態(tài)下對(duì)應(yīng)的值,再通過ng-model 將其與 $scope 中的屬性對(duì)應(yīng),便實(shí)現(xiàn)了type=”checkbox” 的雙向動(dòng)態(tài)綁定。
<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" />籃球
四、完整示例
<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>
以上就是關(guān)于AngularJS單選框及多選框?qū)崿F(xiàn)雙向動(dòng)態(tài)綁定的相關(guān)介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
AngularJS中的Promise詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了AngularJS中的Promise詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,Promise是一種模式,以同步操作的流程形式來操作異步事件,避免了層層嵌套,可以鏈?zhǔn)讲僮鳟惒绞录枰呐笥芽梢詤⒖枷?/div> 2016-12-12angular select 默認(rèn)值設(shè)置方法
下面小編就為大家?guī)硪黄猘ngular select 默認(rèn)值設(shè)置方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06AngularJS實(shí)現(xiàn)的2048小游戲功能【附源碼下載】
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的2048小游戲,可實(shí)現(xiàn)通過鍵盤W、S、A、D鍵控制上下左右移動(dòng)進(jìn)行游戲的功能,涉及AngularJS頁面元素動(dòng)態(tài)操作及數(shù)值運(yùn)算等相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01基于angular實(shí)現(xiàn)模擬微信小程序swiper組件
這篇文章主要介紹了基于angular實(shí)現(xiàn)模擬微信小程序swiper組件 ,需要的朋友可以參考下2017-06-06Angularjs在360兼容模式下取數(shù)據(jù)緩存問題的解決辦法
這篇文章主要為大家詳細(xì)介紹了Angularjs在360兼容模式下取數(shù)據(jù)緩存問題的解決辦法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06如何解決手機(jī)瀏覽器頁面點(diǎn)擊不跳轉(zhuǎn)瀏覽器雙擊放大網(wǎng)頁
這篇文章主要介紹了如何解決手機(jī)瀏覽器頁面點(diǎn)擊不跳轉(zhuǎn)瀏覽器雙擊放大網(wǎng)頁的相關(guān)資料,需要的朋友可以參考下2016-07-07最新評(píng)論