国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS單選框及多選框實現雙向動態綁定

2019-11-20 10:48:37
字體:
來源:轉載
供稿:網友

在AngularJS中提及雙向數據綁定,大家肯定會想到ng-model指令。

一、ng-model

ng-model指令用來將input、select、textarea或自定義表單控件同包含它們的作用域中的屬性進行綁定。它將當前作用域中運算表達式的值同給定的元素進行綁定。如果屬性不存在,它會隱式創建并將其添加到當前作用域中。
始終用ng-model來綁定scope上一個數據模型內的屬性,而不是scope上的屬性,這可以避免在作用域或后代作用域中發生屬性覆蓋!

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

二、type=”radio”

通過 value 屬性指定選中狀態下對應的值,并通過 ng-model 將單選框與 $scope 中的屬性對應,便實現了 type=”radio” 時的雙向動態綁定。

<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 的內置指令 ng-true-value 和 ng-false-value ,指定多選框在選中和未選中狀態下對應的值,再通過ng-model 將其與 $scope 中的屬性對應,便實現了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" />籃球

四、完整示例

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

以上就是關于AngularJS單選框及多選框實現雙向動態綁定的相關介紹,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛教| 东平县| 红桥区| 普格县| 隆昌县| 禹州市| 开鲁县| 丹巴县| 明光市| 延川县| 巴东县| 宜章县| 平江县| 宁明县| 凌海市| 新乐市| 班玛县| 仪陇县| 东乌珠穆沁旗| 岳西县| 垦利县| 河北省| 项城市| 新野县| 阳城县| 龙南县| 勃利县| 中阳县| 中方县| 喜德县| 高州市| 平利县| 信丰县| 仲巴县| 郯城县| 黄骅市| 阿巴嘎旗| 蒙山县| 庆城县| 将乐县| 兴和县|