因为做用域的继承是基于js的原型继承方式,因此这里分为两种状况,看成用域上面的值为基本类型的时候,修改父做用域上面的值会
影响到子做用域,反之,修改子做用域只会影响子做用域的值,不会影响父做用域上面的值;若是须要父做用域与子做用域共享一个值
的话,就须要用到后面一种,即做用域上的值为对象,任何一方的修改都能影响另外一方,这是由于在js中对象都是引用类型。html
基本类型app
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>
对象ide
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>
在通常状况下基于继承的方式已经足够知足大部分状况了,可是这种方式没有实现兄弟控制器之间的通讯方式,因此引出了事件的方式
。基于事件的方式中咱们能够里面做用的$on,$emit,$boardcast这几个方式来实现,其中$on表示事件监听,$emit表示向父级以上的
做用域触发事件, $boardcast表示向子级如下的做用域广播事件。参照如下代码:spa
向上传播事件code
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>
向下广播事件orm
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>
从这个用法咱们能够引伸出一种用于兄弟控制间进行通讯的方法,首先咱们一个兄弟控制中向父做用域触发一个事件,而后在父做用域
中监听事件,再广播给子做用域,这样经过事件携带的参数,实现了数据通过父做用域,在兄弟做用域之间传播。这里要注意的是,经过父元素做为中介进行传递的话,兄弟元素用的事件名不能同样,不然会进入死循环。请看代码:htm
-兄弟做用域之间传播对象
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>
在ng中服务是一个单例,因此在服务中生成一个对象,该对象就能够利用依赖注入的方式在全部的控制器中共享。参照如下例子,在一个控制器修改了服务对象的值,在另外一个控制器中获取到修改后的值:继承
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>