【AngularJS】1.AngularJS MVC 以及$scope作用域

前端之家收集整理的这篇文章主要介绍了【AngularJS】1.AngularJS MVC 以及$scope作用域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.AngularJS MVC


Model:数据模型层

View:视图层,负责展示

Controller:业务逻辑和控制逻辑

优点: 代码模块化 代码逻辑比较清晰、可移值性高,后期维护方便、代码复用, 代码规模越来越大的时候,切分职责是大势所趋
缺点:运行效率稍微低一些

2. Angularjs $scope 作用域
(1)$scope 是Template与Controller之间传值的纽带,是单独的作用域 ,不能通过$scope在多个控制器之间传值。如下代码中,firstController里面的没有定义age值,所以对应的template里面age值不会显示,secondController里面的name值没有定义,所以对应template里面的name值不会显示

<!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){
              $rootScope.age='30';
          })
      </script>
       
    </body>
</html>
(2)$roootScope根作用域,多个控制器之间共享数据,可以通过$roootScope传值,如下代码所示,secondController里面定义的age属性,在firstController对应的Template上age能正常显示。而firstController里面定义的name属性,在secondController对应的Template上就不能正常显示
<!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",$rootScope){
              $rootScope.age='30';
          })
      </script>
       
    </body>
</html>
3.控制器的继承问题

如下代码所示,secondController在firstController里面,secondController里面没有定义name属性,但是对应的Template里面的name可以继承firstController里面的name属性,同样可以正常显示“张三”,而age属性是根作用域$scopeScope传值,所以能够正常显示“30”。

<!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 ng-controller="secondController">
                  {{name}}
                  {{age}}
              </div>
          </div>

      </div>
      <script type="text/javascript">
          var app = angular.module("myApp",$rootScope){
              $rootScope.age='30';
          })
      </script>
       
    </body>
</html>
原文链接:https://www.f2er.com/angularjs/147656.html

猜你在找的Angularjs相关文章