• About Morris Development
  • A Focus for Cloud Efficiency
    • Microsoft Azure
    • Amazon Web Services
  • What our Clients Say
  • Our Products
    • PreschoolDB
    • WebinarDB
  • Contact Us

Morris Development

Custom System Development and Integration

January 22, 2016

Angular Factories with TypeScript

To make our code as easy to maintain as possible, we keep all data interactions in modules called Factories. Most of our factory files have about 10 or 12 callback functions and we try to keep them pretty simple. Moving them to typescript wasn’t very intuitive at first, but once we figured it out, it is a no-brainer.

Here’s a basic sample that we used to get a list of offices from the api server. You can add as many functions as you want. This one just has one.


module Application.Services {

    export interface IMyService {
        GetOffices(successCallback: Function);
    }

    export class MyService {

        http: ng.IHttpService;
        location: ng.ILocationService;

        constructor($http: ng.IHttpService, $location: ng.ILocationService) {
            this.http = $http;
            this.location = $location;
        }

        GetOffices(successCallback: Function) {     
            this.http.get(apiServer + "/api/Office?sid=" + session_id).success((data, status) => {
                successCallback(data,status);
            }).error(error => {
                successCallback(error,"ERROR");
            });
        }
    }
}

app.factory("adminFact", ["$http", "$location", ($http, $location) => new Application.Services.MyService($http, $location)]);

so, here’s how it works: you’ve got your normal factor, but you replace the entire “function” section with a typescript lambda. (slick).
Normally, you’d have something like this:

app.factory('reportFact', function ($http, $q) {
    return {  
        Offices: function () {
            return $http.get(apiServer + "/api/Office?sid=" + session_id);
        }
   };
});
 

So you can see, the factory is returning a function, which you can then run to get your data. With TypeScript, you’re going to make that function ahead of time in a module. (note: We’ve just called this one Application.Services. You can call it FooBar.MyService if you want. Doesn’t matter.)

Ok, so the first thing you’re going to need is the Interface. This is what makes TypeScript understand your object and check it for you as well as provide intellisense.

  export interface IMyService {
        GetOffices(successCallback: Function);
    }

So… any functions you want to have access to, just list them in there.

Then you’ve got to export your class

export class MyService {

You’ll want to define the typs of objects you’re expecting to get in the constructor so you can use them in the rest of the module.

       http: ng.IHttpService;
       location: ng.ILocationService;

Then you’ve got the constructor, that “makes” the object.

  constructor($http: ng.IHttpService, $location: ng.ILocationService) {
            this.http = $http;
            this.location = $location;
        }

Remember, these have to match your creating statement for the factory:

app.factory("adminFact", ["$http", "$location", ($http, $location) => new Application.Services.MyService($http, $location)]);

finally, you’ve got your functions. You can have as many as you want.

  GetOffices(successCallback: Function) {     
            this.http.get(apiServer + "/api/Office?sid=" + session_id).success((data, status) => {
                successCallback(data,status);
            }).error(error => {
                successCallback(error,"ERROR");
            });
        }

Pretty cool, huh?

(note, I have some horrible global variables that are a carry-over from way way back (apiServer and session_id). some day we’ll get them in there properly, but for this particular system, it’s just easy.)

[disqus]

Article by MacGyver / AngularJS, TypeScript

About MacGyver

I've worked with database systems for over 20 years, and started my own company in 2000. Almost all my business consists of internal database systems, either ERP or CRM. My programming is primarily in Angular / Microsoft C# and MS SQL.

About This Site

Morris Development has been specializing in internal database system design and integration since 1999. We provide long-term management and support of secure data systems for many businesses as well as developing the more complex code structures for ERP systems like Intellievent, Apidas, and AVMS.

This site is primarily for our developers to keep track up various technologies and updates that are used by Morris Development.

Training

Integrating Angular Microsite with .Net

Private Data Caching with Google Storage

Continuous Deployment for Production Releases?

Azure Websites – the perfect Angular host

Angular 2

  • Angular 2 Authentication
  • Angular Command Line Interface
  • Material Design for Angular
  • Using Observables in Angular 2

Mentors

  • Ben Nadel
  • Dan Wahlin
  • Deborah Kurata
  • John Papa

Staff

  • Dan Morris

Training

  • Google Development Courses
  • Microsoft Virtual Academy
  • PluralSight
  • Test Deep Links

© 2025 · Morris Development