3.24

来源:互联网 发布:埃文特纳nba数据 编辑:程序博客网 时间:2024/05/23 21:01

范围扮演其视图连接控制器的角色一个特殊的JavaScript对象。范围包含了模型数据。在控制器,模型数据通过$scope对象访问。

<iframe id="cproIframe_u1377264_2" width="728" height="15" src="http://pos.baidu.com/acom?adn=0&amp;at=128&amp;aurl=&amp;cad=1&amp;ccd=32&amp;cec=UTF-8&amp;cfv=11&amp;ch=0&amp;col=zh-CN&amp;conBW=0&amp;conOP=1&amp;cpa=1&amp;dai=2&amp;dis=0&amp;layout_filter=tabcloud%2Cimage&amp;ltr=http%3A%2F%2Fwww.yiibai.com%2Fangularjs%2Fangularjs_expressions.html&amp;ltu=http%3A%2F%2Fwww.yiibai.com%2Fangularjs%2Fangularjs_scopes.html&amp;lu_161=0&amp;lunum=6&amp;n=90029059_cpr&amp;pcs=1349x599&amp;pis=10000x10000&amp;ps=608x349&amp;psr=1366x768&amp;pss=1349x1157&amp;qn=1792ecd4a732d75b&amp;rad=&amp;rsi0=728&amp;rsi1=15&amp;rsi5=4&amp;rss0=%23FFFFFF&amp;rss1=%23FFFFFF&amp;rss2=%230000FF&amp;rss3=&amp;rss4=&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=&amp;td_id=1377264&amp;titFF=%E5%AE%8B%E4%BD%93&amp;titFS=12&amp;titTA=left&amp;tn=tlink_default_728_15&amp;tpr=1427205412389&amp;ts=1&amp;version=2.0&amp;xuanting=0&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u1377264&amp;tt=1427205412374.187.280.280" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box;"></iframe>
<script>      var mainApp = angular.module("mainApp", []);      mainApp.controller("shapeController", function($scope) {         $scope.message = "In shape controller";         $scope.type = "Shape";      });</script>

以下是在上面的例子中需要考虑的重要问题。

  • $scope被作为第一个参数在其构造器确定指标到控制器。

  • $scope.message 和 $scope.type 是它们在HTML页面中所用的模型。

  • 我们已经设置模型的值将反映应用程序模块的控制器shapeController中。

  • 我们可以在$scope定义函数功能。

继承范围

范围是特定的控制器。如果我们定义嵌套的控制器,然后控制器子将继承其父控制的范围。

<script>      var mainApp = angular.module("mainApp", []);      mainApp.controller("shapeController", function($scope) {         $scope.message = "In shape controller";         $scope.type = "Shape";      });        mainApp.controller("circleController", function($scope) {         $scope.message = "In circle controller";         });</script>

以下是在上面的例子中需要考虑的重要问题。

  • 我们在shapeController设定模型的值。

  • 我们覆盖的子控制器circleController消息。当“消息”内的控制器circleController的模块使用时,将用于重写的消息。

例子

下面的例子将展示上述所有指令。

testAngularJS.html
<html><head>   <title>Angular JS Forms</title></head><body>   <h2>AngularJS Sample Application</h2>   <div ng-app="mainApp" ng-controller="shapeController">      <p>{{message}} <br/> {{type}} </p>      <div ng-controller="circleController">         <p>{{message}} <br/> {{type}} </p>      </div>      <div ng-controller="squareController">         <p>{{message}} <br/> {{type}} </p>      </div>   </div>   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>   <script>      var mainApp = angular.module("mainApp", []);      mainApp.controller("shapeController", function($scope) {         $scope.message = "In shape controller";         $scope.type = "Shape";      });      mainApp.controller("circleController", function($scope) {         $scope.message = "In circle controller";         });      mainApp.controller("squareController", function($scope) {         $scope.message = "In square controller";         $scope.type = "Square";      });   </script></body></html>

结果

在Web浏览器打开textAngularJS.html。看到结果如下。

AngularJS Scopes
0 0
原创粉丝点击