亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Angular.js中控制器之間的傳值詳解

 更新時(shí)間:2017年04月24日 11:04:19   作者:quan134  
angular中每個(gè)controller(控制器)都會(huì)有自己的$scope,通過(guò)為這個(gè)對(duì)象添加屬性賦值,就可以將數(shù)據(jù)傳遞給模板進(jìn)行渲染,每個(gè)$scope只會(huì)在自己控制器內(nèi)起作用,而有時(shí)候需要用到其他控制器中的數(shù)據(jù)就要考慮到控制器之間參數(shù)的傳遞了,下面來(lái)看看詳細(xì)的介紹。

前言

每個(gè)controller都會(huì)有自己的scope,所有的scope都是屬于 $rootScope的子或者子的子...

那么問(wèn)題就好解決了,通過(guò) $rootScope.$broadcast 廣播的事件每個(gè)controller都能收到事件

另外,我的經(jīng)驗(yàn)是,盡量不要用event傳數(shù)據(jù)。應(yīng)該建立一個(gè)service來(lái)保存數(shù)據(jù),并且設(shè)置相應(yīng)getter/setter,具體如下:

 每個(gè)controller依賴service, call service.setter(...)

統(tǒng)一的service.setter(...)在改完數(shù)據(jù)后可以$emit('data-updated')

每個(gè)controller里$on('data-updated', function(){ $scope.data = service.getData() })

通過(guò)Angular的話可以通過(guò)下面四種方法

1、event

這里可以有兩種方式,一種是$scope.$emit,然后通過(guò)$rootScope的事件獲取參數(shù);另一種是$rootScope.$broadcast,通過(guò)$scope的事件獲取參數(shù)。

這兩種方法在最新版本的Angular中已經(jīng)沒(méi)有性能區(qū)別了,主要就是事件發(fā)送的方向不同,可以按實(shí)際情況選擇。

2、service

可以創(chuàng)建一個(gè)專用的事件Service,也可以按照業(yè)務(wù)邏輯切分,將數(shù)據(jù)存儲(chǔ)在相應(yīng)的Service中,因?yàn)橐呀?jīng)有人提過(guò)了就不贅述了。

3、$rootScope

這個(gè)方法可能會(huì)比較dirty一點(diǎn),勝在方便,也就是把數(shù)據(jù)存在$rootScope中,這樣各個(gè)子$scope都可以調(diào)用,不過(guò)需要注意一下生命周期

4、直接使用$scope.$nextSibling及類似的屬性

類似的還有$scope.$parent。這個(gè)方法的缺點(diǎn)就更多了,官方不推薦使用任何$開(kāi)頭的屬性,既增加了耦合,又需要處理異步的問(wèn)題,而且scope的順序也不是固定的。不推薦

另外就是通過(guò)本地存儲(chǔ)、全局變量或者現(xiàn)代瀏覽器的postMessage來(lái)傳遞參數(shù)了,除非特殊情況,請(qǐng)避免這類方式。

直接建一個(gè)service,不要用什么事件,項(xiàng)目一大N多個(gè)controller或者N久再去維護(hù)會(huì)玩死你,service里提供存和取的方法,簡(jiǎn)單明了,API容易查找和修改,調(diào)試也方便無(wú)混亂的依賴關(guān)系

1、利用作用域的繼承方式

由于作用域的繼承是基于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>

2、基于事件的方式

在一般情況下基于繼承的方式已經(jīng)足夠滿足大部分情況了,但是這種方式?jīng)]有實(shí)現(xiàn)兄弟控制器之間的通信方式,所以引出了事件的方式 ?;谑录姆绞街形覀兛梢岳锩孀饔玫?on,$emit,$boardcast這幾個(gè)方式來(lái)實(shí)現(xiàn),其中$on表示事件,$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è)事件,然后在父作用域中事件,再?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>

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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者使用Angular.js能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論