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="+" /> <div ng-controller="LevelB"><hr> <h3>{{title}}</h3> ValueA = {{valueA}}<br> ValueB = {{valueB}} <input type="button" ng-click="inc()" value="+" /> <div ng-controller="LevelC"><hr> <h3>{{title}}</h3> ValueA = {{valueA}}<br> ValueB = {{valueB}}<br> ValueC = {{valueC}} <input type="button" ng-click="inc()" value="+" /> </div> </div></div><script src="js/angular-1.3.0.js"></script><script src="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++; } })
时间: 2024-10-24 20:53:34