范圍扮演其視圖連接控制器的角色一個特殊的JavaScript對象。范圍包含了模型數(shù)據(jù)。在控制器,模型數(shù)據(jù)通過$scope對象訪問。
<script> var mainApp = angular.module("mainApp", []); mainApp.controller("shapeController", function($scope) { $scope.message = "In shape controller"; $scope.type = "Shape"; });</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"; });</script>以下是在上面的例子中需要考慮的重要問題。
例子
下面的例子將展示上述所有指令。
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>結(jié)果
在Web瀏覽器打開textAngularJS.html。看到結(jié)果如下。

新聞熱點(diǎn)
疑難解答