수

AngularJS Scope 3

· 2017-08-07 (월) 12:47:22 · 1859
AngularJS Scope

범위는 HTML (보기)와 JavaScript (컨트롤러) 간의 바인딩 부분입니다.

범위는 사용 가능한 속성 및 메서드가있는 객체입니다.

범위는 뷰와 컨트롤러 모두에서 사용할 수 있습니다.


귀하의 범위를 아십시오.
언제든지 어떤 범위를 다루고 있는지 파악하는 것이 중요합니다.

위의 두 예제에는 하나의 범위 만 있으므로 범위를 아는 것은 문제가되지 않지만 큰 응용 프로그램의 경우 HTML DOM에 특정 범위에만 액세스 할 수있는 섹션이있을 수 있습니다.


예
ng-repeat지시어를 처리 할 때 각 반복은 현재 반복 객체에 액세스 할 수 있습니다

<div ng-app="myApp" ng-controller="myCtrl">

<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
</script>

전체소스
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
</script>

<p>The variable "x" has a different value for each repetition, proving that each repetition has its own scope.</p>

</body>
</html>

각 <li>요소는 현재 반복 객체에 액세스 할 수 있습니다.이 경우에는 using으로 참조되는 문자열 x입니다.


루트 범위
모든 응용 프로그램에는 지시문 $rootScope이 포함 된 HTML 요소에서 작성된 범위가 있습니다 ng-app.

rootScope는 전체 응용 프로그램에서 사용할 수 있습니다.

변수가 현재 범위와 rootScope에서 동일한 이름을 갖는 경우 응용 프로그램은 현재 범위에있는 변수를 사용합니다.

예
"color"라는 변수는 컨트롤러 범위와 rootScope에 존재합니다 :

<body ng-app="myApp">

<p>The rootScope's favorite color:</p>
<h1>{{color}}</h1>

<div ng-controller="myCtrl">
<p>The scope of the controller's favorite color:</p>
<h1>{{color}}</h1>
</div>

<p>The rootScope's favorite color is still:</p>
<h1>{{color}}</h1>

<script>
var app = angular.module('myApp', []);
app.run(function($rootScope) {
$rootScope.color = 'blue';
});
app.controller('myCtrl', function($scope) {
$scope.color = "red";
});
</script>
</body>

전체소스
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myApp">

<p>The rootScope's favorite color:</p>
<h1>{{color}}</h1>

<div ng-controller="myCtrl">

<p>The scope of the controller's favorite color:</p>
<h1>{{color}}</h1>

</div>

<p>The rootScope's favorite color is still:</p>
<h1>{{color}}</h1>

<script>
var app = angular.module('myApp', []);
app.run(function($rootScope) {
$rootScope.color = 'blue';
});
app.controller('myCtrl', function($scope) {
$scope.color = "red";
});
</script>

<p>Notice that controller's color variable does not overwrite the rootScope's color value.</p>

</body>
</html>
|
댓글을 작성하시려면 로그인이 필요합니다.

퍼블리싱강좌

302건
+
분류 제목 글쓴이 날짜 조회
AngularJS 17-08-09 조회 2,558
AngularJS 17-08-09 조회 1,870
AngularJS 17-08-08 조회 1,595
AngularJS 17-08-08 조회 2,354
AngularJS 17-08-08 조회 2,373
AngularJS 17-08-07 조회 1,860
AngularJS 17-08-07 조회 1,959
AngularJS 17-08-07 조회 2,091
AngularJS 17-08-06 조회 1,865
AngularJS 17-08-06 조회 1,694
AngularJS 17-08-06 조회 1,887
AngularJS 17-08-04 조회 2,035
AngularJS 17-08-04 조회 1,931
AngularJS 17-08-04 조회 2,135
AngularJS 17-08-03 조회 1,633
AngularJS 17-08-03 조회 1,781
AngularJS 17-08-03 조회 1,632
AngularJS 17-08-02 조회 2,382
AngularJS 17-08-02 조회 2,047
AngularJS 17-08-02 조회 2,198
AngularJS 17-08-01 조회 1,917
AngularJS 17-08-01 조회 2,031
AngularJS 17-08-01 조회 2,173
AngularJS 17-07-28 조회 1,967
AngularJS 17-07-28 조회 2,779
AngularJS 17-07-28 조회 1,967
jQuery Mobile
[jQuery Mobile]
17-07-26 조회 2,780
jQuery Mobile
[jQuery Mobile]
17-07-26 조회 2,538
jQuery Mobile
[jQuery Mobile]
17-07-26 조회 2,622
jQuery Mobile 17-07-25 조회 2,268