AngularJS - Scope Scope là đối tượng JavaScript đặc biệt có vai trò liên kết controller và view.. Trong controller, dữ liệu model có thể được truy cập qua đối tượng $scope.. controller
Trang 1AngularJS - Scope Scope là đối tượng JavaScript đặc biệt có vai trò liên kết controller và view Scope chứa thông tin
là các dữ liệu model Trong controller, dữ liệu model có thể được truy cập qua đối tượng $scope
<script>
var mainApp = angular module ("mainApp", []);
mainApp controller ("shapeController", function ( $scope ) {
$scope message = "In shape controller";
$scope type = "Shape";
});
</script>
Dưới đây là những điểm quan trọng của ví dụ trên
shapeController
Tính kế thừa của Scope
Scope là controller riêng biệt Chúng ta định nghĩa nested controller (các controller lồng nhau) để các controller con sẽ kế thừa từ các controller cha
<script>
var mainApp = angular module ("mainApp", []);
mainApp controller ("shapeController", function ( $scope ) {
$scope message = "In shape controller";
$scope type = "Shape";
});
Trang 2
mainApp controller ("circleController", function ( $scope ) {
$scope message = "In circle controller";
});
</script>
Dưới đây là những điểm chính qua ví dụ trên
trong các module của circleController, giá trị message ghi đè sẽ được sử dụng
Ví d ụ
Dưới đây là phần ví dụ cho phần hướng dẫn bên trên
testAngularJS.jsp
<html>
<head>
<title>Angular JS Forms</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app ="mainApp" ng-controller ="shapeController" >
<p>{{message}} <br/> {{type}} </p>
<div ng-controller ="circleController" >
<p>{{message}} <br/> {{type}} </p>
</div>
<div ng-controller ="squareController" >
<p>{{message}} <br/> {{type}} </p>
</div>
</div>
<script
src ="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" ></script>
<script>
var mainApp = angular module ("mainApp", []);
Trang 3mainApp controller ("shapeController", function ( $scope ) {
$scope message = "In shape controller";
$scope type = "Shape";
});
mainApp controller ("circleController", function ( $scope ) {
$scope message = "In circle controller";
});
mainApp controller ("squareController", function ( $scope ) {
$scope message = "In square controller";
$scope type = "Square";
});
</script>
</body>
</html>
Mở trang textAngularJS.jsp trên trình duyệt web Và xem kết quả dưới đây