1 <body ng-app=""> 2 3 <div ng-controller="firstController"> 4 <input type="text" ng-model="name" value=""/> 5 {{name}} 6 {{age}} 7 <script> 8 var firstController = function($scope){ 9 //scope叫做作用于 10 $scope.name = "张三"; 11 $scope.age = "20"; 12 } 13 </script> 14 </div> 15 </body>
多个控制器:
1 <body ng-app=""> 2 3 <div ng-controller="firstController"> 4 <input type="text" ng-model="name" value=""/> 5 <div ng-controller="secondController"> 6 <input type="text" ng-model="name" value=""/> 7 </div> 8 </div> 9 10 11 <script> 12 var firstController = function($scope){ 13 $scope.name = "张三"; 14 } 15 var secondController = function($scope){ 16 //$scope.name="李四" 17 } 18 19 </script> 20 </body>