angularjs學(xué)習(xí)筆記之雙向數(shù)據(jù)綁定
這次我們來(lái)詳細(xì)講解angular的雙向數(shù)據(jù)綁定。
一.簡(jiǎn)單的例子
這個(gè)例子我們?cè)诘谝还?jié)已經(jīng)展示過(guò)了,要看的移步這里
這里實(shí)現(xiàn)的效果就是,在輸入框輸入內(nèi)容,下面也會(huì)相應(yīng)的改變對(duì)應(yīng)的內(nèi)容。這就實(shí)現(xiàn)了數(shù)據(jù)雙向綁定。
二.取值表達(dá)式與ng-bind的使用
我們?cè)倏匆粋€(gè)例子,點(diǎn)擊這里,文中出現(xiàn)的第一個(gè)例子中,{{greeting.text}}和{{text}}就是一個(gè)取值表達(dá)式了,但是如果你一直刷新頁(yè)面,你會(huì)發(fā)現(xiàn)這樣一個(gè)問(wèn)題,那就是頁(yè)面有時(shí)候會(huì)一瞬間的出現(xiàn)“{{greeting.text}} {{text}}”這個(gè)字符串,那我們?cè)撊绾谓鉀Q呢?
這里就用到ng-bind命令了:用于綁定數(shù)據(jù)表達(dá)式。
比如我們可以把
<p>{{greeting.text}} {{text}}</p>
改為:
"<p><span ng-bind="greeting.text"></span><span ng-bind="text"></span></p>";
這樣改正之后,頁(yè)面刷新就不會(huì)有不希望出現(xiàn)的字符串出現(xiàn)了。
但是使用命令總要比直接使用表達(dá)式的效率低一點(diǎn),所以我們總結(jié)了一個(gè)常用規(guī)律:一般來(lái)說(shuō),index使用ng-bind,后續(xù)模版中的使用'{{}}'的形式。
三.雙向綁定的典型場(chǎng)景-表單
先看一個(gè)form.html的內(nèi)容:
<!doctype html> <html ng-app="UserInfoModule"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css"> <script src="js/angular-1.3.0.js"></script> <script src="Form.js"></script> </head> <body> <div class="panel panel-primary"> <div class="panel-heading"> <div class="panel-title">雙向數(shù)據(jù)綁定</div> </div> <div class="panel-body"> <div class="row"> <div class="col-md-12"> <form class="form-horizontal" role="form" ng-controller="UserInfoCtrl"> <div class="form-group"> <label class="col-md-2 control-label"> 郵箱: </label> <div class="col-md-10"> <input type="email" class="form-control" placeholder="推薦使用126郵箱" ng-model="userInfo.email"> </div> </div> <div class="form-group"> <label class="col-md-2 control-label"> 密碼: </label> <div class="col-md-10"> <input type="password" class="form-control" placeholder="只能是數(shù)字、字母、下劃線" ng-model="userInfo.password"> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <div class="checkbox"> <label> <input type="checkbox" ng-model="userInfo.autoLogin">自動(dòng)登錄 </label> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <button class="btn btn-default" ng-click="getFormData()">獲取Form表單的值</button> <button class="btn btn-default" ng-click="setFormData()">設(shè)置Form表單的值</button> <button class="btn btn-default" ng-click="resetForm()">重置表單</button> </div> </div> </form> </div> </div> </div> </div> </body> </html>
再看Form.js的內(nèi)容:
var userInfoModule = angular.module('UserInfoModule', []); userInfoModule.controller('UserInfoCtrl', ['$scope', function($scope) { $scope.userInfo = { email: "253445528@qq.com", password: "253445528", autoLogin: true }; $scope.getFormData = function() { console.log($scope.userInfo); }; $scope.setFormData = function() { $scope.userInfo = { email: 'testtest@126.com', password: 'testtest', autoLogin: false } }; $scope.resetForm = function() { $scope.userInfo = { email: "253445528@qq.com", password: "253445528", autoLogin: true }; } } ])
實(shí)現(xiàn)效果截圖如下:
上圖實(shí)現(xiàn)的功能是:
1.點(diǎn)擊”獲取“,可以在控制臺(tái)輸出三個(gè)數(shù)據(jù),郵箱、密碼和選中狀態(tài)(true、false)
2.點(diǎn)擊“設(shè)置”:可以更改兩個(gè)輸入框的值和復(fù)選框取消選中的狀態(tài);
3.點(diǎn)擊“重置”:可以讓數(shù)據(jù)恢復(fù)到初始數(shù)據(jù)。
因?yàn)檩斎肟蛑械膎g-model和控制器中的數(shù)值實(shí)現(xiàn)了雙向綁定,所以更改輸入框的值或者更改控制器中的值,都會(huì)相應(yīng)的更改雙方的值。就這么幾行代碼,就實(shí)現(xiàn)了這么強(qiáng)大的功能,是不是覺(jué)得很神奇呢?確實(shí)很神奇,不過(guò),更加神奇的還在后面呢!繼續(xù)吧!
四.動(dòng)態(tài)切換標(biāo)簽樣式
先看color.html的內(nèi)容:
<!doctype html> <html ng-app="MyCSSModule"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="CSS1.css"> </head> <style type="text/css"> .text-red { background-color: #ff0000; } .text-green { background-color: #00ff00; } </style> <body> <div ng-controller="CSSCtrl"> <p class="text-{{color}}">測(cè)試CSS樣式</p> <button class="btn btn-default" ng-click="setGreen()">綠色</button> </div> </body> <script src="js/angular-1.3.0.js"></script> <script src="color.js"></script> </html>
我們看第19行:有一個(gè)“color”的變量存在于這個(gè)p標(biāo)簽中,當(dāng)點(diǎn)擊“綠色”時(shí),執(zhí)行setGreen函數(shù),改變“color”的值為“green”,所以更改了類(lèi)名,從而也更改了背景顏色。使用這樣的方法,讓我們不用去直接操作元素,而是加一個(gè)變量就行了。代碼簡(jiǎn)潔直觀。
我們?cè)倏匆幌耤olor.js的內(nèi)容:
var myCSSModule = angular.module('MyCSSModule', []); myCSSModule.controller('CSSCtrl', ['$scope', function($scope) { $scope.color = "red"; $scope.setGreen = function() { $scope.color = "green"; } } ])
屬性“color”的默認(rèn)值為“red”,所以顯示紅色,點(diǎn)擊時(shí)執(zhí)行函數(shù),變?yōu)榫G色。
- 詳談AngularJs 控制器、數(shù)據(jù)綁定、作用域
- 深入淺析AngularJS中的一次性數(shù)據(jù)綁定 (bindonce)
- AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解
- AngularJS框架中的雙向數(shù)據(jù)綁定機(jī)制詳解【減少需要重復(fù)的開(kāi)發(fā)代碼量】
- AngularJS入門(mén)教程之?dāng)?shù)據(jù)綁定原理詳解
- AngularJS入門(mén)教程之?dāng)?shù)據(jù)綁定用法示例
- AngularJS 雙向數(shù)據(jù)綁定詳解簡(jiǎn)單實(shí)例
- AngularJS實(shí)踐之使用NgModelController進(jìn)行數(shù)據(jù)綁定
- 詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
- Angularjs中數(shù)據(jù)綁定的實(shí)例詳解
相關(guān)文章
AngularJS整合Springmvc、Spring、Mybatis搭建開(kāi)發(fā)環(huán)境
這篇文章主要介紹了AngularJS整合Springmvc、Spring、Mybatis搭建開(kāi)發(fā)環(huán)境的相關(guān)資料,為學(xué)習(xí)使用AngularJS做好基礎(chǔ)準(zhǔn)備,感興趣的小伙伴們可以參考一下2016-02-02AngularJS $on、$emit和$broadcast的使用
本文主要介紹AngularJS $on、$emit和$broadcast的使用,這里整理了詳細(xì)的資料及簡(jiǎn)單示例代碼有興趣的小伙伴可以參考下2016-09-09詳解使用angular-cli發(fā)布i18n多國(guó)語(yǔ)言Angular應(yīng)用
這篇文章主要介紹了詳解使用angular-cli發(fā)布i18n多國(guó)語(yǔ)言Angular應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05妙用Angularjs實(shí)現(xiàn)表格按指定列排序
使用AngularJS的過(guò)濾器,可以很容易的實(shí)現(xiàn)在表格中,點(diǎn)擊某一列標(biāo)題進(jìn)行排序,實(shí)現(xiàn)代碼也很簡(jiǎn)單,下面小編給大家分享angularjs實(shí)現(xiàn)表格按指定列排序的實(shí)現(xiàn)代碼,需要的的朋友參考下吧2017-06-06Angularjs通過(guò)指令監(jiān)聽(tīng)ng-repeat渲染完成后執(zhí)行腳本的方法
指令是angular的核心功能之一,用好了事半功倍,監(jiān)聽(tīng)ng-repeat執(zhí)行狀態(tài)僅僅是它功能的冰山一角吧。下面這篇文章主要介紹了Angularjs通過(guò)指令監(jiān)聽(tīng)ng-repeat渲染完成后執(zhí)行腳本的方法,需要的朋友可以參考下。2016-12-12Angular.JS讀取數(shù)據(jù)庫(kù)數(shù)據(jù)調(diào)用完整實(shí)例
這篇文章主要介紹了Angular.JS讀取數(shù)據(jù)庫(kù)數(shù)據(jù)調(diào)用,結(jié)合完整實(shí)例形式分析了AngularJS使用$http.get方法與后臺(tái)php交互讀取數(shù)據(jù)庫(kù)數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下2019-07-07Material(包括Material Icon)在Angular2中的使用詳解
這篇文章主要介紹了Material(包括Material Icon)在Angular2中的使用,需要的朋友可以參考下2018-02-02