|
| 1 | +<!DOCTYPE html> |
| 2 | +<html ng-app="exampleApp"> |
| 3 | +<head> |
| 4 | + <title>AngularJS Demo</title> |
| 5 | + <link href="bootstrap.css" rel="stylesheet" /> |
| 6 | + <link href="bootstrap-theme.css" rel="stylesheet" /> |
| 7 | + <script src="angular.js"></script> |
| 8 | + <script> |
| 9 | + |
| 10 | + var myApp = angular.module("exampleApp", []); |
| 11 | + |
| 12 | + myApp.controller("dayCtrl", function ($scope, days) { |
| 13 | + $scope.day = days.today; |
| 14 | + }); |
| 15 | + |
| 16 | + myApp.controller("tomorrowCtrl", function ($scope, days) { |
| 17 | + $scope.day = days.tomorrow; |
| 18 | + }); |
| 19 | + |
| 20 | + myApp.directive("highlight", function ($filter) { |
| 21 | + |
| 22 | + var dayFilter = $filter("dayName"); |
| 23 | + |
| 24 | + return function (scope, element, attrs) { |
| 25 | + if (dayFilter(scope.day) == attrs["highlight"]) { |
| 26 | + element.css("color", "red"); |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + myApp.filter("dayName", function () { |
| 32 | + var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", |
| 33 | + "Thursday", "Friday", "Saturday"]; |
| 34 | + return function (input) { |
| 35 | + return angular.isNumber(input) ? dayNames[input] : input; |
| 36 | + }; |
| 37 | + }); |
| 38 | + |
| 39 | + // 이 부분에서는 변수를 선언하고 해당 변수가 갖는 값을 지정하게 된다. |
| 40 | + // Module.value 매소드는 고정값 및 객체를 반환하는 서비스를 하게 되고, |
| 41 | + // 이를 활용해 service나 filter 같은 모듈 매소드를 통해 생성되는 객체뿐 아니라 모든 값이나 객체에 대해 의존성 주입을 활용할 수 있게 되므로 그만큼 편하다. |
| 42 | + // 한마디로 좀 더 일관적인 개발 기법을 적용하게 해주고 단위 테스트를 단순화 함 |
| 43 | + var now = new Date(); |
| 44 | + myApp.value("nowValue", now); |
| 45 | + // now라는 변수를 선언하고, 이 변수에 Date를 대입하고 |
| 46 | + // nowValue라는 value 서비스를 생성했다. |
| 47 | + // days 서비스를 선언할 때 nowValue 서비스를 의존성으로 선언해 사용한다. |
| 48 | + |
| 49 | + myApp.service("days", function (nowValue) { |
| 50 | + this.today = nowValue.getDay(); |
| 51 | + this.tomorrow = this.today + 1; |
| 52 | + }); |
| 53 | + |
| 54 | + // myApp.service("days", function () { |
| 55 | + // this.today = now.getDay(); |
| 56 | + // this.tomorrow = this.today + 1; |
| 57 | + // }); |
| 58 | + // Value를 사용할 생각이 없다면, |
| 59 | + // 클로저 기능을 활용하여 구현할 수도 있다. |
| 60 | + |
| 61 | + |
| 62 | + </script> |
| 63 | +</head> |
| 64 | +<body> |
| 65 | + <div class="panel"> |
| 66 | + <div class="page-header"> |
| 67 | + <h3>AngularJS App</h3> |
| 68 | + </div> |
| 69 | + <h4 ng-controller="dayCtrl" highlight="Monday"> |
| 70 | + Today is {{day || "(unknown)" | dayName}} |
| 71 | + </h4> |
| 72 | + <h4 ng-controller="tomorrowCtrl"> |
| 73 | + Tomorrow is {{day || "(unknown)" | dayName}} |
| 74 | + </h4> |
| 75 | + </div> |
| 76 | +</body> |
| 77 | +</html> |
0 commit comments