未加星标

Creating Angular Controllers using TypeScript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Recently I decided to move one of my project to typescript, project was getting bigger was getting harder to maintain, and managing big projects becomes even more problematic if tooling is not great, I mean, there are lot of IDE's that does the great job but it's still not on par with the tooling we get for other languages such as php , .NET .

So I decided to use TypeScript, which is nothing but a superset of javascript, in fact valid JavaScript is also valid TypeScriptso I just changed all of my files extensions from .js to .ts and started to convert the project from one by one, and within a week, I successfully migrated from JavaScript to TypeScript.

Angular 2 is knocking the door although you can write your Angular2 apps in ES5, ES6, but TypeScript is the preferred way so it's a good time to get a hang of it.

I will discuss my typescript workflow in upcoming tutorials but for this tutorial I will talk about angular controllers using TypeScript.

What We are going to build

In this very simple example, we are going to create A Controller which will simply add or subtract value and save it in the cookie.

Let's start with writing Controller in JavaScript MathController in JavaScript( mathcontroller.js) angular.module('app').controller('MathController', ['$cookieStore', function($cookieStore) {
var vm = this;
vm.total = 0;
vm.addToTotal = function () {
vm.total = vm.total + 1;
saveTotalToCookie(vm.total);
}
vm.subtractFromTotal = function() {
vm.total = vm.total - 1;
saveTotalToCookie(vm.total);
}
function saveTotalToCookie(total) {
$cookieStore.put('total', total);
}
}]);

As you can see we have two functions to add and tosubtract from total which is initialized as 0, we are also injecting $cookieStore in this Controller, now let's write the same functionality in TypeScript.

MathController in TypeScript( mathcontroller.ts ) interface IMathController {
total : number;
addToTotal() : void;
subtractToTotal() : void;
saveTotalToCookie(total : number) : void;
}
class MathController implements IMathController {
static $inject = ['$cookieStore'];
total:number;
constructor( private $cookieStore : ng.cookies.ICookieStoreService) {this.total = 0;
}
addToTotal():void {this.total = this.total + 1;this.saveTotalToCookie(this.total);
}
subtractToTotal():void {this.total = this.total -1;this.saveTotalToCookie(this.total);
}
saveTotalToCookie(total : number ):void {this.$cookieStore.put('total', total);
}
}
angular.module('devArt').controller('MathController', MathController); Let's break it down into Pieces

As you can see we are starting with the interface which is part of the TypeScript and only usedfor development purpose, and will not be included in transpiled JS output. This interface will make sure that we implement all the method and properties in the Class which is implementing this interface( in this case MathController ).

interface IMathController {
total : number;
addToTotal() : void;
subtractToTotal() : void;
saveTotalToCookie(total : number) : void;
}

Now we are going to create the MathController class and injecting "$cookieStore" service as a static property named $inject , also let's save it as class property in the constructor.

class MathController implements IMathController {
static $inject = ['$cookieStore'];
constructor( private $cookieStore : ng.cookies.ICookieStoreService) {}
}

We are implementing IMathController so we must create three-member methods and one member property to fulfil the contract, let's also initialize member property total with 0 in the constructor.

class MathController implements IMathController {
static $inject = ['$cookieStore'];
total:number;
constructor( private $cookieStore : ng.cookies.ICookieStoreService) {
this.total = 0;
}
addToTotal():void {
this.total = this.total + 1;
this.saveTotalToCookie(this.total);
}
subtractToTotal():void {
this.total = this.total -1;
this.saveTotalToCookie(this.total);
}
saveTotalToCookie(total : number ):void {
this.$cookieStore.put('total', total);
}
}

Finally, we need to Register the controller which is business as always.

angular.module('app').controller('MathController', MathController);

Note:Controller registration must come after the class not before, or else it will generate an error.

That's it, guys, it might seem a lot more trouble at first but later down the road it will make our project lot more manageable and easier to maintain and extend finally if you have any questions ask us below in the comment section or head over to ourforum andcreate new thread.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaJavaScriptPHP
分页:12
转载请注明
本文标题:Creating Angular Controllers using TypeScript
本站链接:http://www.codesec.net/view/484757.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(26)