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

相关文章
相关标签/搜索