首页 > 代码库 > Angular_(作用域层次结构)

Angular_(作用域层次结构)

html部分
.........................................................................................
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div ng-controller="LevelA">
<h3>{{title}}</h3>
ValueA = {{valueA}} <input type="button" ng-click="inc()" value="http://www.mamicode.com/+" />
<div ng-controller="LevelB"><hr>
<h3>{{title}}</h3>
ValueA = {{valueA}}<br>
ValueB = {{valueB}}
<input type="button" ng-click="inc()" value="http://www.mamicode.com/+" />
<div ng-controller="LevelC"><hr>
<h3>{{title}}</h3>
ValueA = {{valueA}}<br>
ValueB = {{valueB}}<br>
ValueC = {{valueC}}
<input type="button" ng-click="inc()" value="http://www.mamicode.com/+" />
</div>
</div>
</div>
<script src="http://www.mamicode.com/js/angular-1.3.0.js"></script>
<script src="http://www.mamicode.com/js/作业3_作用域继承.js"></script>
</body>
</html>
...........................................................................
js部分
myApp=angular.module("myApp",[]);
myApp.controller("LevelA",function ($scope,$rootScope) {
$scope.valueA=0; //初始化
$scope.valueB=0; //初始化
$scope.valueC=0; //初始化
$scope.title="LevelA";

$scope.inc=function () {
$scope.valueA++;
$rootScope.valueB++;
$rootScope.valueC++;
}
})
myApp.controller("LevelB",function ($scope,$rootScope) {
$scope.title="LevelB";
$scope.inc=function () {
$scope.valueB++;
$rootScope.valueC++;
}
})
myApp.controller("LevelC",function ($scope) {
$scope.title="LevelC";
$scope.inc=function () {
$scope.valueC++;
}

})




Angular_(作用域层次结构)