2014/05/25追è¨
ããããæ¸ãæ¹ãåºæ¥ãã¨ç´¹ä»ãã¦ãããã¾ããã
@okazuki $scopeã®ã¡ã½ããã®åãFunctionã«ããããªãå ´å URL ãã¼ãã¼æãã§æ¸ãã¨ã³ããã§æ¸ã+åãã§ãã¯ã®æ©æµãåããããã®ã§ããã¨æãã¾ãã
2014-05-25 02:06:40 via twicli to @okazuki
ã¯ããã«
JavaScriptã§SPAä½ãã®ã«ã¯AngularJSããããããã¨ãããã¨ã§ãã¨ããããã·ã³ãã«ãªä¾ã¨ãã¦ãåå¼·ãã¦ã以ä¸ã®ãã¼ã¸ã®ããã£ã±ãªã«ããTodoã¢ããªãTypeScriptã§æ¸ãã¦ã¿ã¾ããã
ããã¸ã§ã¯ãã®ä½æ
TypeScriptHTMLApp1ã¨ããååï¼ååã¤ããã®ãã¼ã£ãï¼ã§ããã¸ã§ã¯ããä½æãã¦ãNuGetãã以ä¸ã®ã©ã¤ãã©ãªã追å ãã¾ãã
- angularjs
- angularjs.TypeScript.DefinitelyTyped
- Twitter.Bootstrap
Twitter.Bootstrapã¯ãè¦ãç®ãã¡ãã£ã¨å¤ãããããªã¨æã£ãããã§ãã
ã³ã³ããã¼ã©ã®ä½æ
AngularJSãTypeScriptã§ä½ãã«ã¯ãã¨ãããã以ä¸ã®æé ãè¸ãã®ãããããã«æãã¾ããã
- 使ç¨ãããã¼ã¿ã®åã®ä½æ
- $scopeç¨ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®ç¾©
- ã³ã³ããã¼ã©ã®ä½æ
é çªã«Todoã¢ããªã§ãã£ã¦ã¿ã¾ãã
ä»åã®Todoã¢ããªã§ã¯ãããã¹ãã¨å®äºãããã©ããããã¼ã¿ã¨ãã¦æã¤ã®ã§ããã®ã¯ã©ã¹ãå®ç¾©ãã¾ããã¯ã©ã¹åã¯TodoItemã«ãã¾ããã
class TodoItem { text: string; done: boolean; }
ããã¦ãã¹ã³ã¼ããå®ç¾©ãã¾ããã¹ã³ã¼ãã¯ng.IScopeãæ¡å¼µããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ãã¦å®ç¾©ãã¾ããã¤ã³ã¿ã¼ãã§ã¼ã¹åã¯TodoScopeã«ãã¾ããã
interface TodoScope extends ng.IScope { todos: Array<TodoItem>; todoText: string; addTodo: Function; remaining: Function; archive: Function; }
é¢æ°ã¯ãå ¨é¨Functionã¨ãã¦åæå®ããã®ããå人çã«ã¡ãã£ã¨æ°ã«å ¥ããªãã§ãã
ããã¦ãã³ã³ããã¼ã©ã¯ã©ã¹ã§ããã³ã³ããã¼ã©ã¯ãã³ã³ã¹ãã©ã¯ã¿ã§ã¹ã³ã¼ããåãåããã¹ã³ã¼ãã«å¿ è¦ãªãã¼ã¿ã®è¨å®ã¨ãã¹ã³ã¼ãã«ã¡ã½ããã®å®ä½ãè¨å®ãã¾ãã
class TodoCtrl { constructor(private $scope: TodoScope) { $scope.todos = [ { text: "AngularJSã®å¦ç¿", done: true }, { text: "AngularJSã®ã¢ããªã±ã¼ã·ã§ã³æ§ç¯", done: false } ]; $scope.addTodo = angular.bind(this, this.addTodo); $scope.remaining = angular.bind(this, this.remaining); $scope.archive = angular.bind(this, this.archive); } addTodo(): void { this.$scope.todos.push({ text: this.$scope.todoText, done: false }); this.$scope.todoText = ""; } remaining(): number { var count = 0; angular.forEach(this.$scope.todos, (todo: TodoItem) => { count += todo.done ? 0 : 1; }); return count; } archive(): void { var old = this.$scope.todos; this.$scope.todos = []; angular.forEach(old, (todo: TodoItem) => { if (!todo.done) { this.$scope.todos.push(todo); } }); } }
angular.bindã使ã£ã¦ã³ã³ããã¼ã©ã«å®ç¾©ããã¡ã½ããã®thisããã¤ã³ããã¦ã¹ã³ã¼ãã®ã¡ã½ããã«è¨å®ãã¦ãã¨æããã¾ãã
app.tsã®å ¨ä½ã¯ä»¥ä¸ã®ããã«ãªãã¾ããã
class TodoItem { text: string; done: boolean; } interface TodoScope extends ng.IScope { todos: Array<TodoItem>; todoText: string; addTodo: Function; remaining: Function; archive: Function; } class TodoCtrl { constructor(private $scope: TodoScope) { $scope.todos = [ { text: "AngularJSã®å¦ç¿", done: true }, { text: "AngularJSã®ã¢ããªã±ã¼ã·ã§ã³æ§ç¯", done: false } ]; $scope.addTodo = angular.bind(this, this.addTodo); $scope.remaining = angular.bind(this, this.remaining); $scope.archive = angular.bind(this, this.archive); } addTodo(): void { this.$scope.todos.push({ text: this.$scope.todoText, done: false }); this.$scope.todoText = ""; } remaining(): number { var count = 0; angular.forEach(this.$scope.todos, (todo: TodoItem) => { count += todo.done ? 0 : 1; }); return count; } archive(): void { var old = this.$scope.todos; this.$scope.todos = []; angular.forEach(old, (todo: TodoItem) => { if (!todo.done) { this.$scope.todos.push(todo); } }); } }
Viewã®ä½æ
Viewã¯ç¹ã«ä¾ã¨å¤ããããapp.cssã«Todoã®ãã§ãã¯ãå ¥ã£ãã¨ãã®ã¹ã¿ã¤ã«ãä¾ã®éãå®ç¾©ãã¾ãã
.done-true { text-decoration: line-through; color: gray; }
htmlã¯Twitter Bootstrapã使ãããã«ããç¹ä»¥å¤ã¯ããµã³ãã«ã¨åãã§ãã
<!DOCTYPE html> <html lang="ja" ng-app> <head> <meta charset="utf-8" /> <title>Todo sample app</title> <link rel="stylesheet" href="app.css" type="text/css" /> <link href="Content/bootstrap.min.css" rel="stylesheet" /> <script src="Scripts/jquery-1.9.0.min.js"></script> <script src="Scripts/bootstrap.min.js"></script> <script src="Scripts/angular.min.js"></script> <script src="app.js"></script> </head> <body> <div class="jumbotron"> <h1>Todo</h1> </div> <div class="container" ng-controller="TodoCtrl"> <div class="row"> <div class="col-md-1"> <span>æ®ã:{{remaining()}}/{{todos.length}}</span> </div> <div class="col-md-1"> [<a href="" ng-click="archive()">å®äº</a>] </div> </div> <div class="row"> <div class="col-md-12"> <ul> <li ng-repeat="todo in todos"> <input type="checkbox" ng-model="todo.done" /> <span class="done-{{todo.done}}">{{todo.text}}</span> </li> </ul> </div> </div> <div class="row"> <div class="col-md-12"> <form ng-submit="addTodo()"> <input type="text" ng-model="todoText" size="30" placeholder="æ°ããTODOã追å " /> <input class="btn" type="submit" value="追å " /> </form> </div> </div> </div> </body> </html>
{{ }}ã使ã£ã¦ãã¤ã³ãããããng-***ã§è²ã ããã¿ããã§ãããVisual Studioã§ã{{ }}ã®ä¸ã¾ã§ã¯è£å®ãã¦ãããªãã®ã§ãã¡ãã£ã¨ã¹ãã¬ã¹â¦ã
å®è¡çµæ
ããæãã®Todoã«ãªãã¾ããã
ããã ãã§ãã³ã¬ã¯ã·ã§ã³ã®ãã¤ã³ãã¨ãã§ããã®ã¯å¼·åã§ããã
ã¾ã¨ã
èªåã§è²ã ããã®ã«æ¯ã¹ãããããã¤ã使ãã®ãããã®ããªãã¨æã£ããæããªãã£ãããã§ãã¨ã£ãããã¯ãããæããªã®ã§ãããå°ããã£ã¦ã¿ããã¨æãã¾ãã