I'm really loving the changes introduced in ES2015 (otherwise known as ES6 or the new JavaScript). At work we've transitioned to working in ES2015, and discovering the differences has been both fun and challenging, especially when it comes to changes in how variables are scoped. But we'll save some of that for another day, and talk for a moment about passing data around in an Angular application, and how you can have some fun with ES2015 classes.

In Angular, a Controller can have Services as dependencies. Services are singleton in nature, so a Service shared among multiple components can be used to "share" data, to a degree. First, let's create a simple controller with a few dependencies and a custom variable we want to track both within the controller, and among other bits of the app.

view plain print about
1'use strict';
2    
3    class MyController {
4        constructor ($scope, dataService, orderService) {
5            this.$scope = $scope;
6            this._dataSvc = dataService;
7            this._orderSvc = orderService;
8            
9            this.myCrazyVar = null;
10            dataService.myCrazyVar = this.myCrazyVar;
11        }
12    }
13    
14    myController.$inject = ['$scope', 'dataService', 'orderService'];
15    
16    export {MyController};

Next, let's create some simple method in the orderService that needs access to our custom variable. We can easily do this with the shared dataService:

view plain print about
1'use strict';
2    
3    class OrderService {
4        constructor (dataService) {
5            this._dataSvc = dataService;
6        }
7        
8        add (order) {
9            // get our custom var for quick reference
10            let forNow = this._dataSvc.myCrazyVar;
11            // do a bunch of stuff, then
12            forNow.OrderId = order.id;
13            // give our changes back to our dataService
14            this._dataSvc.myCrazyVar = forNow;
15        }
16    }
17    
18    orderService.$inject = ['dataService'];
19    
20    export {OrderService};

First, some might ask "Cutter, why didn't you just pass that variable into the method?" Well, sometimes you just can't. Others might ask "Isn't it passed by reference?" Scoping changes have adjusted how this works as well. Changing the variable in the service won't automatically update your controller variable. We'll talk more about that in a moment. Still other's might ask "What is 'let'?" That's a conversation about the differences in variable assignment in ES2015, and is really a discussion for another day. Ultimately I used let because I need the variable to be mutable.

But, to explain what I'm doing here, the add() method takes an order. What you can see of the method, it gets our custom variable and applies it to a local variable for easy reference. We update it with data from the order that was passed in. We then reset the Service property with the updated data.

OK, but that syntax with the Service property is odd, coming off of ES5. How does that work? Well, ES2015 classes allow for implicit getters and setters of properties. Consider the following:

view plain print about
1'use strict';
2    
3    class DataService {
4        constructor () {
5            this.myCrazyVar = null;
6        }
7    }
8    
9    export {DataService};

In the past, to access and change a property of an object would have required us to write some kind of getter or setter method. In this example, you can simply access and change the property directly through dot notation. But let's say you want to do something a bit more complex. There may be some bit of pre- or post-process you want to do, either when setting or getting the variable. For this, ES2015 classes allow you to define custom getters and setters:

view plain print about
1'use strict';
2    
3    class DataService {
4        constructor () {
5            this._myCrazyVar = null;
6        }
7        
8        set myCrazyVar (value) {
9            // I can do what I want in here
10            this._myCrazyVar = value;
11        }
12        
13        get myCrazyVar () {
14            // I could do stuff here too, if I wanted
15            return this._myCrazyVar;
16        }
17    }
18    
19    export {DataService};

Where this could come in handy is in that inter-app communication. A Controller can call methods on the Service, to set values and stuff, but the Service can't automatically pass data back to the Controller based on actions from other items accessing the Service (like our orderService interaction above). But, in Angular, we can use event handling and binding in this instance. First, let's put a listener on our controller:

view plain print about
1constructor ($scope, dataService, orderService) {
2        this.$scope = $scope;
3        this._dataSvc = dataService;
4        this._orderSvc = orderService;
5        
6        this.myCrazyVar = null;
7        this._dataSvc.myCrazyVar = this.myCrazyVar;
8        $scope.$on('crazyUpdated', ($event, newValue) =>
this.myCrazyVar = newValue);
9    }

Yes, the arrow function is a different concept for most front-end-only developers. I'm not going in depth on that here, but you can find plenty of info out there about them. The gist here is that if the crazyUpdated event is cast it will pass a new value, that we then use to update the Controller variable. This also tells us that myCrazyVar will always be changed from outside of the Controller. Now let's do some magic to make sure that event gets cast. In our dataService:

view plain print about
1import {MyController} from './MyController';
2    import {DataService} from './DataService';
3    import {OrderService} from './OrderService';
4    
5    // ... other app init stuff
6    .controller('MyController', MyController)
7    .service('dataService', DataService)
8    .service('orderService', OrderService);

So we use our custom property setter. When the value is changed, it automatically broadcasts that change. The Controller then picks up that change, and applies it to it's own internal variable.

So, what have we learned here? Well, first there's some samples on using ES2015 classes as Controllers and Services within Angular. Simple examples, but there you go. Next, we talked about class property getters and setters, both implicit (no need to define, they just work), and explicit. Our explicit example shows where you can apply some additional logic during those processes. This may not be the greatest example, but it shows you that you can do stuff. Probably the greatest usages here will be in data validation, or in splitting concatenated data (like a full name to first and last, for example).

Using those ES6 classes in your app is a matter of importing the classes into your app:

view plain print about
1import {MyController} from './MyController';
2    import {DataService} from './DataService';
3    import {OrderService} from './OrderService';
4    
5    // ... other app init stuff
6    .controller('MyController', MyController)
7    .service('dataService', DataService)
8    .service('orderService', OrderService);

So, it's still all new to me, these changes to scope and classes and the like. But it's fun, and powerful, and has a ton of possibilities. If I screwed something up just let me know. All feedback is welcome.