<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script>//MVC //function Aaa($scope){ // $scope.name = 'hello';//Model,$scope是对象name是对象的属性,scope是作用域范围是ng-controller="Aaa"标签范围。 // $scope.age = '20'; //} function Aaa($scope,$rootScope){//$rootScope是作用域是全局作用域范围是整个页面, $scope.name = 'hello'; $scope.age = '40';//局部作用域覆盖全局作用域 $rootScope.age = '20'; } function Bbb($scope){ $scope.name = 'hi'; $scope.age = '30'; } </script> </head> <body> <div ng-controller="Aaa"> //Control,ng-controller是作用域,范围是div范围。 <p>{{name}}</p> //View <div ng-controller="Bbb"> <p>{{name}}</p> <p>{{age}}</p> </div> </div> </body> </html>
<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> //传统函数的定义与调用 function show(n){ } show(2); function Aaa($scope,$rootScope){//传入了局部作用域的对象和全局作用域的对象 alert(123); } </script> </head> <body> <div ng-controller="Aaa"> //写了这个就会执行Aaa函数, </div> </body> </html>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5734563.html,如需转载请自行联系原作者