+ All Categories
Home > Technology > using AngularJS with TypeScript

using AngularJS with TypeScript

Date post: 15-Jan-2015
Category:
Upload: david-pichsenmeister
View: 10,326 times
Download: 2 times
Share this document with a friend
Description:
A short introduction, how to use the AngularJS Framework with the trending language TypeScript. It includes a short intro to TypeScript, DefinitelyTyped and of course some example code.
16
by david pichsenmeister using AngularJS with TypeScript
Transcript
Page 1: using AngularJS with TypeScript

by david pichsenmeister

using AngularJS with TypeScript

Page 2: using AngularJS with TypeScript

TypeScript

● TypeScript is a typed superset of JavaScript that compiles to plain JavaScript (ECMAScript3-compatible).

● TypeScript adds optional types, classes, modules and generics

● (only) compile-time type checking

Page 3: using AngularJS with TypeScript

TypeScript

you can

● use existing JavaScript code

● incorporate popular JavaScript libraries

● call it from other JavaScript code

Page 4: using AngularJS with TypeScript

TypeScript

● can be installed as a node.js packagenpm install -g typescript

● compiled with TypeScript compilertsc helloworld.ts

● reference files with /// <reference path='path/to/file.ts' />

Page 5: using AngularJS with TypeScript

DefinitelyTyped

● repository for high-quality TypeScript type definitions

● type definitions for all common frameworks and libraries

● has its own package manager for definitions (tsd)

Page 6: using AngularJS with TypeScript

TSD - TypeScript Definition Manager

● install via npmnpm install -g tsd

● install definitionstsd query angular --resolve --action install

Page 8: using AngularJS with TypeScript

reference all files

/// <reference path='typings/jquery/jquery.d.ts' />

/// <reference path='typings/angularjs/angular.d.ts' />

/// <reference path='typings/angularjs/angular-route.d.ts' />

//##### models #####

/// <reference path='models/ScaffoldModel.ts' />

//##### services #####

/// <reference path='services/ScaffoldService.ts' />

//##### directives #####

/// <reference path='directives/ScaffoldDirective.ts' />

//##### controllers #####

/// <reference path='controllers/ScaffoldCtrl.ts' />

/// <reference path='app.ts' />

Page 9: using AngularJS with TypeScript

app.ts

/// <reference path='_all.ts' />

module app {

'use strict'

var myapp: ng.IModule = angular.module( 'app', ['ngRoute'])

myapp.controller( 'ctrl', ScaffoldCtrl.prototype.injection())

myapp.service( 'service', ScaffoldService.prototype.injection())

myapp.directive( 'directive', ScaffoldDirective.prototype.injection())

myapp.config([ '$routeProvider', function($routeProvider: ng.route.IRouteProvider) {

$routeProvider.when( '/home', {templateUrl: 'partials/home.html'}).

otherwise({redirectTo: '/home'})

}])

}

Page 10: using AngularJS with TypeScript

model

/// <reference path='../_all.ts' />

module app {

'use strict'

export class ScaffoldModel {

constructor(){}

}

}

Page 11: using AngularJS with TypeScript

controller

/// <reference path='../_all.ts' />

module app {

'use strict'

export class ScaffoldCtrl {

public injection(): Array<any> {

return [

ScaffoldCtrl

]

}

constructor() {}

}

}

Page 12: using AngularJS with TypeScript

service

/// <reference path='../_all.ts' />

module app {

'use strict'

export class ScaffoldService {

public injection(): Array<any> {

return [

ScaffoldService

]

}

constructor() {}

}

}

Page 13: using AngularJS with TypeScript

directive 1/2

/// <reference path='../_all.ts' />

module app {

'use strict'

export class ScaffoldDirective implements ng.IDirective {

public injection(): Array<any> {

return [

() => { return new ScaffoldDirective() }

]

}

Page 14: using AngularJS with TypeScript

directive 2/2 public templateUrl: string

public restrict: string

constructor() {

this.templateUrl = 'partials/templates/directive.html'

this.restrict = 'E'

}

public link ($scope: ng.IScope, element: JQuery, attributes: ng.IAttributes): void {

element.text("i'm a directive")

}

}

}


Recommended