本文實(shí)例講述了AngularJS控制器之間的通信方式。分享給大家供大家參考,具體如下:
一、利用作用域的繼承方式
由于作用域的繼承是基于js的原型繼承方式,所以這里分為兩種情況,當(dāng)作用域上面的值為基本類型的時(shí)候,修改父作用域上面的值會(huì)影響到子作用域,反之,修改子作用域只會(huì)影響子作用域的值,不會(huì)影響父作用域上面的值;如果需要父作用域與子作用域共享一個(gè)值的話,就需要用到后面一種,即作用域上的值為對(duì)象,任何一方的修改都能影響另一方,這是因?yàn)樵趈s中對(duì)象都是引用類型。
基本類型
function Sandcrawler($scope) { $scope.location = "Mos Eisley North"; $scope.move = function(newLocation) { $scope.location = newLocation; }}function Droid($scope) { $scope.sell = function(newLocation) { $scope.location = newLocation; }}
html:
<div ng-controller="Sandcrawler"> <p>Location: {{location}}</p> <button ng-click="move('Mos Eisley South')">Move</button> <div ng-controller="Droid"> <p>Location: {{location}}</p> <button ng-click="sell('Owen Farm')">Sell</button> </div></div>
對(duì)象
function Sandcrawler($scope) { $scope.obj = {location:"Mos Eisley North"};}function Droid($scope) { $scope.summon = function(newLocation) { $scope.obj.location = newLocation; }}
html:
<div ng-controller="Sandcrawler"> <p>Sandcrawler Location: {{location}}</p> <div ng-controller="Droid"> <button ng-click="summon('Owen Farm')"> Summon Sandcrawler </button> </div></div>
二、基于事件的方式
在一般情況下基于繼承的方式已經(jīng)足夠滿足大部分情況了,但是這種方式?jīng)]有實(shí)現(xiàn)兄弟控制器之間的通信方式,所以引出了事件的方式。基于事件的方式中我們可以里面作用的$on,$emit,$boardcast這幾個(gè)方式來(lái)實(shí)現(xiàn),其中$on表示事件監(jiān)聽(tīng),$emit表示向父級(jí)以上的作用域觸發(fā)事件, $boardcast表示向子級(jí)以下的作用域廣播事件。參照以下代碼:
向上傳播事件
function Sandcrawler($scope) { $scope.location = "Mos Eisley North"; $scope.$on('summon', function(e, newLocation) { $scope.location = newLocation; });}function Droid($scope) { $scope.location = "Owen Farm"; $scope.summon = function() { $scope.$emit('summon', $scope.location); }}
html:
<div ng-controller="Sandcrawler"> <p>Sandcrawler Location: {{location}}</p> <div ng-controller="Droid"> <p>Droid Location: {{location}}</p> <button ng-click="summon()">Summon Sandcrawler</button> </div></div>
向下廣播事件
function Sandcrawler($scope) { $scope.location = "Mos Eisley North"; $scope.recall = function() { $scope.$broadcast('recall', $scope.location); }}function Droid($scope) { $scope.location = "Owen Farm"; $scope.$on('recall', function(e, newLocation) { $scope.location = newLocation; });}
html:
<div ng-controller="Sandcrawler"> <p>Sandcrawler Location: {{location}}</p> <button ng-click="recall()">Recall Droids</button> <div ng-controller="Droid"> <p>Droid Location: {{location}}</p> </div></div>
從這個(gè)用法我們可以引申出一種用于兄弟控制間進(jìn)行通信的方法,首先我們一個(gè)兄弟控制中向父作用域觸發(fā)一個(gè)事件,然后在父作用域中監(jiān)聽(tīng)事件,再?gòu)V播給子作用域,這樣通過(guò)事件攜帶的參數(shù),實(shí)現(xiàn)了數(shù)據(jù)經(jīng)過(guò)父作用域,在兄弟作用域之間傳播。這里要注意的是,通過(guò)父元素作為中介進(jìn)行傳遞的話,兄弟元素用的事件名不能一樣,否則會(huì)進(jìn)入死循環(huán)。請(qǐng)看代碼:
兄弟作用域之間傳播
function Sandcrawler($scope) { $scope.$on('requestDroidRecall', function(e) { $scope.$broadcast('executeDroidRecall'); });}function Droid($scope) { $scope.location = "Owen Farm"; $scope.recallAllDroids = function() { $scope.$emit('requestDroidRecall'); } $scope.$on('executeDroidRecall', function() { $scope.location = "Sandcrawler" });}
html:
<div ng-controller="Sandcrawler"> <div ng-controller="Droid"> <h2>R2-D2</h2> <p>Droid Location: {{location}}</p> <button ng-click="recallAddDroids()">Recall All Droids</button> </div> <div ng-controller="Droid"> <h2>C-3PO</h2> <p>Droid Location: {{status}}</p> <button ng-click="recallAddDroids()">Recall All Droids</button> </div></div>
三、angular服務(wù)的方式
在ng中服務(wù)是一個(gè)單例,所以在服務(wù)中生成一個(gè)對(duì)象,該對(duì)象就可以利用依賴注入的方式在所有的控制器中共享。參照以下例子,在一個(gè)控制器修改了服務(wù)對(duì)象的值,在另一個(gè)控制器中獲取到修改后的值:
var app = angular.module('myApp', []);app.factory('instance', function(){ return {};});app.controller('MainCtrl', function($scope, instance) { $scope.change = function() { instance.name = $scope.test; };});app.controller('sideCtrl', function($scope, instance) { $scope.add = function() { $scope.name = instance.name; };});
html:
<div ng-controller="MainCtrl"> <input type="text" ng-model="test" /> <div ng-click="change()">click me</div></div><div ng-controller="sideCtrl"> <div ng-click="add()">my name {{name}}</div></div>
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注