angular.js 이용

Posted 2013. 11. 18. 10:32

http://angularjs.org/


여기에서 js 를 다운받거나 cdn 주소를 이용.


클라이언트에서 처리하는 언어. 서버가 필요할 때는 ajax 요청.

SPA.

MVC 모델





angular.js 로 todo 리스트 코딩.



index.html

<!doctype html>

<html ng-app>

<head>

<meta charset="utf-8">

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>

<link rel="stylesheet" href="todo.css">

<script type="text/javascript" src="todo.js"></script>

</head>


<body>

<h2>할일</h2>

<div ng-controller="TodoCtrl">

<span>{{todos.length}} 중 {{remaining()}} 남음!!</span>

[ <a href="" ng-click="archive()">정리</a> ]

<ul>

<li ng-repeat="todo in todos">

<input type="checkbox" ng-model="todo.done">

<span class="done-{{todo.done}}">{{todo.text}}</span>

</li>

</ul>

<form ng-submit="addTodo()">

<input type="text" ng-model="todoText" size="30" placeholder="추가할 할일을 입력하세요">

<input type="submit" value="등록">

</form>

</div>

</body>

</html>



todo.js

function TodoCtrl($scope){

$scope.todos = [

{text:'angular.js 공부', done:true},

{text:'angular.js 앱 만들기', done:false},

];


$scope.remaining = function() {

var count = 0;

angular.forEach($scope.todos, function(todo){

count += todo.done ? 0 : 1;

});

return count;

};


$scope.addTodo = function() {

$scope.todos.push({text:$scope.todoText, done:false});

$scope.todoText = '';

};


$scope.archive = function() {

var oldTodos = $scope.todos;

$scope.todos = [];

angular.forEach(oldTodos, function(todo) {

if(!todo.done) {

$scope.todos.push(todo);

}

})

};


}



todo.css

.done-true {

text-decoration: line-through;

color: lightgray;

}