Bài 3 $scope trong controller

Lượt xem: 2471

Xem demo

Biến $scope là một đối tượng (object) lưu trữ dữ liệu cầu nối, hoặc chúng ta có thể hiểu nó như là một kho lưu trữ dữ liệu mà các đối tượng khác như Controller, Service sẽ sử dụng và trao đổi thông tin qua nó.

Làm thế nào để định nghĩa $scope trong Javascript ?

 <script>  
 myWebApp = angular.module('myWebApp', []);     
 myWebApp.controller("Controller01", function($scope){   
  $scope.message = 'Chào mừng đến với blog';});     
myWebApp.controller("Controller02", function($scope){   
 $scope.message = 'Chia sẻ các bài viết cơ bản về Angularjs';});     
 </script>  

Sử dụng nó trong HTML như thế nào?

<body ng-app="myWebApp">    
 <div ng-controller="Controller01">    {{message}}  </div>    
  <div ng-controller="Controller02">    {{message}}  </div>  
 </body>  

Xem thêm:  Bài 4 $rootScope trong controller

Xem demo

Có thể bạn quan tâm