수

AngularJS 모듈 만들기

· 2017-08-02 (수) 12:29:00 · 2381
AngularJS 모듈 만들기

AngularJS 함수를 사용하여 모듈을 만듭니다. angular.module

<div ng-app="myApp">...</div>

<script>

var app = angular.module("myApp", []);

</script>

"myApp"매개 변수는 응용 프로그램이 실행될 HTML 요소를 나타냅니다.

이제 AngularJS 응용 프로그램에 컨트롤러, 지시문, 필터 등을 추가 할 수 있습니다.

컨트롤러 추가하기(Adding a Controller)

응용 프로그램에 제어기를 추가하고 ng-controller지시문 을 사용하여 제어기를 참조하십시오 .

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>

var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});

</script>

컨트롤러에 대한 자세한 내용은이 튜토리얼의 뒷부분에서 설명합니다.

지침 추가(Adding a Directive)
AngularJS에는 응용 프로그램에 기능을 추가하는 데 사용할 수있는 내장 지시문 세트가 있습니다.

<div ng-app="myApp" w3-test-directive></div>

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

app.directive("w3TestDirective", function() {
return {
template : "I was made in a directive constructor!"
};
});
</script>

파일의 모듈 및 컨트롤러(Modules and Controllers in Files)
AngularJS 애플리케이션에서는 모듈과 컨트롤러를 JavaScript 파일에 두는 것이 일반적입니다.

이 예제에서 "myApp.js"는 응용 프로그램 모듈 정의를 포함하고 "myCtrl.js"는 컨트롤러를 포함합니다.


<!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">
{{ firstName + " " + lastName }}
</div>

<script src="myApp.js"></script>
<script src="myCtrl.js"></script>

</body>
</html>

myApp.js
var app = angular.module("myApp", []);


모듈 정의의 [] 매개 변수를 사용하여 종속 모듈을 정의 할 수 있습니다.

[] 매개 변수가 없으면 새 모듈을 만들지 않고 기존 모듈을 검색 합니다.

myCtrl.js
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName= "Doe";
});

함수는 전역 네임 스페이스를 폴링 할 수 있습니다.
전역 함수는 JavaScript에서 피해야합니다. 다른 스크립트로 쉽게 덮어 쓰거나 파괴 할 수 있습니다.

AngularJS 모듈은 모든 기능을 모듈에 로컬로 유지함으로써이 문제를 줄입니다.

라이브러리로드시기
HTML 응용 프로그램에서는 <body>요소 의 끝에 스크립트를 배치하는 것이 일반적이지만 AngularJS 라이브러리를 <head>시작시 또는 시작할 때로드하는 것이 좋습니다 <body>.

이것은 angular.module라이브러리가로드 된 후에 만 호출을 컴파일 할 수 있기 때문 입니다.

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

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>

</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,859
AngularJS 17-08-07 조회 1,958
AngularJS 17-08-07 조회 2,091
AngularJS 17-08-06 조회 1,865
AngularJS 17-08-06 조회 1,694
AngularJS 17-08-06 조회 1,886
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,916
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