Angular Services in Aurelia

Aurelia 1 Javascript

In Angular 1.x you had the concept of a controller, directive, service, provider and other confusing terms to describe essentially what is one or two things.

What is an Angular service
An Angular service no matter how it was defined is a singleton. Every part of your application gets the same reference when it requests a service or factory.

Angular services in Aurelia
The concept of controllers, services, providers and factories does not exist in Aurelia. Everything is just an ECMAScript 2015 class with hints of future specification features like decorators thrown in for good measure. This works in our favour because porting an Angular service to Aurelia requires very little to no effort.

An example service in Angular

app.service('MyService', function() {
    return {
        getData: function(url) {
            
        },

        postData: function(url, data) {
            
        }
    }
});

Porting it to Aurelia

export class MyService {
    getData(url) {
        
    }

    postData(url, data) {
        
    }
}

Honestly as you can see, there is really no effort in porting over an Angular service to Aurelia. It almost warrants not even needing a post, but this seems to be a reoccurring question that it needed to be written.

Then using your service simply requires including and injecting it into whatever ViewModel you need to use it in.

Using Async/Await In Aurelia

One of the biggest upcoming additions to Javascript (in my opinion) is support for async/await. If you have worked with a language like C# before, …

Isomorphic Aurelia Applications

Update March 2018: Two years on from the original publish date, Aurelia finally has support for server-side rendering. This means it is now possible …