angularjs scope rootscope 區別

scope是html和單個controller之間的橋樑,數據綁定就靠他了。rootsope是各個controller中scope的橋樑。用rootscope定義的值,能夠在各個controller中使用。下面用實例詳細的說明一下。html

一、js代碼this

phonecatApp.controller('TestCtrl',['$scope','$rootScope',  
    function($scope,$rootScope) {  
        $rootScope.name = 'this is test';  
    }  
]);  
  
phonecatApp.controller('Test111Ctrl',['$scope','$rootScope',  
    function($scope,$rootScope) {  
        $scope.name = $rootScope.name;  
    }  
]);

二、htmlspa

<div ng-controller="TestCtrl">  
    I  set the global variable.<strong>{{$root.name}}</strong>  
</div>  
  
<div ng-controller="Test111Ctrl">  
    1,get global variable .<strong>{{name}}</strong><br>  
    2,get global variable .<strong>{{$root.name}}</strong>  
</div>

三、輸出結果code

  1. I set the global variable.this is test  htm

  2. 1,get global variable .this is test  get

  3. 2,get global variable .this is test  io

四、總結:function

$rootScope.name設置的變量,在全部controller裏面都是能夠直接用{{$root.name}}來顯示的,很強大。那固然也能夠賦值給scope。class

相關文章
相關標籤/搜索