Questions And Answers

More Tutorials

Angular Controllers in Typescript

When a Controller is attached to the DOM via the ng-controller directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. A new child scope will be created and made available as an injectable parameter to the Controller's constructor function as $scope.

Controllers can be very easily made using the typescript classes.

module App.Controllers {
class Address {
 line1: string;
 line2: string;
 city: string;
 state: string;
 export class SampleController {
 firstName: string;
 lastName: string;
 age: number;
 address: Address;
 setUpWatches($scope: ng.IScope): void {
 $scope.$watch(() => this.firstName, (n, o) => {
 //n is string and so is o
 constructor($scope: ng.IScope) {

The Resulting Javascript is

var App;
(function (App) {
 var Controllers;
 (function (Controllers) {
 var Address = (function () {
 function Address() {
 return Address;
 var SampleController = (function () {
 function SampleController($scope) {
 SampleController.prototype.setUpWatches = function ($scope) {
 var _this = this;
 $scope.$watch(function () { return _this.firstName; }, function (n, o) {
 //n is string and so is o
 return SampleController;
 Controllers.SampleController = SampleController;
 })(Controllers = App.Controllers || (App.Controllers = {}));
})(App || (App = {}));

After making the controller class let the angular js module about the controller can be done simple by using the class


 .controller('exampleController', App.Controller.SampleController)


In this page (written and validated by ) you learned about AngularJS Controllers in Typescript . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS http request.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.