<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body ng-app="myApp"> <div ng-controller="firstController"> {{name}} {{nickName}} <!--继承上面的--> <div ng-controller="threeContrller"> {{name}} {{nickName}} </div> </div> <div ng-controller="secondController"> {{sex}} {{nickName}} </div> <script src="angular/angular.js"></script> <script type="text/javascript"> angular.module("myApp",[]) .controller("firstController",['$scope',function($s){ /*依赖注入中的代码的压缩*/ $s.name="张三"; }]) .controller("secondController",function($scope,$rootScope){ /*$rootScope一个全局变量,作用于在任何控制器之上*/ $scope.sex="男"; $rootScope.nickName="刘德华"; /*挂载在$rootScope中的nickName是在全局作用域中,可以在任何控制器内使用*/ }) .controller("threeContrller",['$scope',function($scope){ $scope.name='03controller'; /*这个控制器在firstController内部,继承与firstController,挂载在$scope中的name会覆盖掉父级的name*/ }]) </script> </body> </html>