AngularJs – Typescript integrieren

Tutorial AngularJs

von Ardian Shala, 30.04.2016 - update: 19.05.2017
TypeScript Klassen für AngularJs
Vorraussetzung:
  • Angular 1.5.x - link
  • TS Compiler
  • Editor (Visual Studio/Code)

Downloads:

Beispiel

Hauptmodul

angular.module('app', ['ngRoute']);

angular.module('app').config(function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'views/home.html',
        controller: 'HomeController'
    });

    $routeProvider.otherwise({
        redirectTo: '/'
    });
});

Controller

class HomeController {
   static $inject = ['$scope'];
   constructor(private $scope: ng.IScope) {
       ($scope).vm = this;
       this.title = "hello Typescript";
   }
   public title: string;
}
angular.module('app').controller('HomeController', HomeController);

Interface

interface ITest {
   name: string;
}

Service

class TestService {
    constructor() {
    }
}

angular.module('app').service("TestService", TestService);

Directive

var TestFactory = (): ng.IDirective => {
   return new Test();
}

class Test implements ng.IDirective {
   constructor() {
   }
   public restrict = 'EA';
   public templateUrl = 'directives/testTemplate.html';
   public scope = {
   }

   public link = (scope: ng.IScope, instanceElement: ng.IAugmentedJQuery, instanceAttributes: ng.IAttributes, transclude: ng.ITranscludeFunction) => {
   }
}

angular.module('app').directive("test", TestFactory);
#angularjs #Basics #javascript #typescript #typestrong #visualstudio

Autor: Ardian Shala

Ersteller der Webseite MuchaDev. Selbstständiger IT Constultant für Frontend Technologien. Full Stack Entwickler mit AngularJs, Angular, Ionic und C#.