zoukankan      html  css  js  c++  java
  • AngularJS的$rootScope和$scope联系和区别

      scope是html和单个controller之间的桥梁,数据绑定就靠他了。

      rootscope是各个controller中scope的桥梁。用rootscope定义的值,可以在各个controller中使用。

      直接上代码

    <!DOCTYPE html>
    <html ng-app="myApp">
        <head>
            <meta charset="UTF-8">
            <title>scope</title>
            <script src="../js/angular.js"></script>
        </head>
        
        <body>
            <div ng-controller="TestCtrl">  
                $root.name是<strong>{{$root.name}}</strong>  
            </div>  
              
            <div ng-controller="Test111Ctrl">  
                $root.name是<strong>{{name}}</strong><br>  
                $scope.name是<strong>{{$root.name}}</strong>  
            </div>     
            <script>
                var app = angular.module('myApp', []);
                app.controller('TestCtrl',function($scope,$rootScope) {  
                        $rootScope.name = 'this is test';  
                    }  
                );  
                  
                app.controller('Test111Ctrl',function($scope,$rootScope) {  
                        $scope.name = $rootScope.name;  
                    }  
                ); 
            </script>
        </body>
        
        
    </html>

    注意一点:在html代码里面用$root取$rootScope的属性方法

  • 相关阅读:
    hdoj_1016Prime Ring Problem
    毛玻璃
    HDOJ1175连连看
    const小结
    指向二维数组的指针
    关于对ACM OJ大数据递归栈溢出问题的解决方案
    Hessian的使用与介绍
    Tomcat调优
    ant使用
    ant使用
  • 原文地址:https://www.cnblogs.com/myhusky/p/5318796.html
Copyright © 2011-2022 走看看