1. 程式人生 > >angularjs1-2,作用域、代碼壓縮

angularjs1-2,作用域、代碼壓縮

初始化 tex sco http 字符 text code rootscope lar

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
        <script type="text/javascript" src="angular.min.js"></script>
    </head>
    <body>
      <div 
ng-app="myApp"> <div ng-controller="firstController"> {{name}} {{age}} {{sex}} <div ng-controller="secondController"> {{name}} {{age}} {{sex}} //secondController作用域先在自己作用域查找,然後再去firstController查找,firstController也沒有就去rootScope查找。
</div> </div> {{name}} {{age}} {{sex}} </div> <script type="text/javascript"> var app = angular.module("myApp", []); //代碼壓縮不會把字符串壓縮,只會把函數的形參壓縮,因此前面寫全,後面可以簡寫。 app.controller(firstController,[$scope
,function($s){ $s.name=張三2; }]); app.controller(secondController,[$scope,$rootScope,function($s,$r){ $s.name=李四; $r.age=30; }]); app.run([$rootScope,function($r){ $r.name=ggggg; $r.age=3333; $r.sex=; }]); console.log(app); </script> </body> </html>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
        <script type="text/javascript" src="angular.min.js"></script>
    </head>
    <body>
      <div ng-app="myApp">
          <div ng-controller="firstController">
              {{name}}
              {{age}}
          </div>
          <div ng-controller="secondController">
              {{name}}
              {{age}}
          </div>
      </div>
      <script type="text/javascript">
          var app = angular.module("myApp", []);
          app.controller(firstController,function($scope){
              $scope.name=張三;
          });
          app.controller(secondController,function($scope,$rootScope){
             // $scope.name=‘李四‘;
              $rootScope.age=30;
          })
      </script>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
        <script type="text/javascript" src="../../angular.min.js"></script>
    </head>
    <body>
      <div ng-app="myApp">
          <div ng-controller="firstController">
              {{name}}
          </div>
          <div ng-controller="secondController">
              {{name}}
          </div>
      </div>
      <script type="text/javascript">
          var app = angular.module("myApp", []);
          app.controller(firstController,function($scope){//scope的作用域是controller作用域
              $scope.name=張三;
          });
          app.controller(secondController,function($scope){
              $scope.name=李四;
          })
      </script>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
        <script type="text/javascript" src="../../angular.min.js"></script>
    </head>
    <body>
      <div ng-app="myApp">
          <div ng-controller="firstController">
             姓名: {{name}} <br>
             年齡:{{age}}
          </div>
          <div ng-controller="secondController">
              姓名:{{name}}
              年齡:{{age}}
          </div>
      </div>
      <script type="text/javascript">
          var app = angular.module("myApp", []);
          app.controller(firstController,function($scope,$rootScope){//rootScope作用域是ng-app作用域
              $scope.name=張三;
              $rootScope.age=30;
          });
          app.controller(secondController,function($scope){
              $scope.name=李四;
          })
      </script>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
        <script type="text/javascript" src="../angular.min.js"></script>
    </head>
    <body>
      <div ng-app="myApp">
          <div ng-controller="firstController">
              {{name}}
              {{age}}
              {{sex}}
              <div ng-controller="secondController">
                  {{name}}
                  {{age}}
                  {{sex}}   //裏層的作用域會去找外層的作用域
              </div>
          </div>
      </div>
      <script type="text/javascript">
          var app = angular.module("myApp", []);
          app.controller(firstController,function($scope){
              $scope.name=張三1;
              $scope.age=40;
          });
          app.controller(secondController,function($scope){
              $scope.name=李四;
              $scope.sex=;
          })
      </script>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>無標題文檔</title>
        <script type="text/javascript" src="../angular.min.js"></script>
    </head>
    <body>
      <div ng-app="myApp">
          <div>
              <p>{{name}}</p>
          </div>
      </div>
      <script type="text/javascript">
          var m1 = angular.module(myApp,[]);
          m1.controller(Aaa,[$scope,function($scope){
                $scope.name = hello111;
           }]);
           m1.controller(Bbb,[$scope,function($scope){
                $scope.name = hi;
           }]);
          m1.run([$rootScope,function($rootScope){
          //run方法初始化全局數據,只對全局作用域起作用。
              $rootScope.name = hello;
          }]);
          console.log( m1 );
      </script>
    </body>
</html>

angularjs1-2,作用域、代碼壓縮