The Many Benefits of AngularJS’s ControllerAs

ControllerAs has been around for quite some time, but I hadn’t given it much thought until recently. You see, I was having issues with $parent and the isolated scopes of 3rd party directives. As if that weren’t enough, I recently read that AngularJS 2 will no longer have $scope. If you have been following AngularJS closely, you will have heard by now that they are currently developing a new major version and it is a game changer. Anyways, I needed a fix to my $parent conundrum and what I found actually has prepared me for the coming death of $scope. We will mourn our loss, dear friends, but at least we can prepare and put our affairs (and applications) slightly in order.

AngularJS’s $scope service provides us with a lot of key functionality in 1.x. You use it from binding your controllers to your views, binding/triggering events and even use it to control how Angular’s $digest cycle. So far, it has been incredibly useful…until you try to get a custom callback to function (pun intended) when someone clicks the “open” button for an Angular UI datepicker. Suddenly you run into an issue where after the user selects a date, they can no longer open the datepicker control. A typical fix? Change the datepicker popup’s isOpen attribute to use the $parent service:


<!-- Awesome fake input tag -->

<input is-open="$parent.opened" />

Problem solved, right? Wrong. You’ve just tightly coupled your datepicker popup to its parent controller’s scope, bub, and you cannot move the datepicker in that controller’s scope hierarchy without reintroducing the same issue you originally solved. Blegh. So what’s a young, resourceful web guru like yourself left to do? Use the ControllerAs functionality that is built into AngularJS 1.1.5+.

Setup your controller to use ‘this’ instead of $scope:


// Awesome controller code:

this.today = function() {
return new Date();
};

//etc...

Bootstrap your controller with an alias like ‘main’ (or something a LOT more semantic, if anyone else is ever going to read your code. Seriously, do everyone a favor. The same goes for class, function, variable and file names), and use that alias to bind to anything you exposed from the controller using the ‘this’ keyword:


<div ng-controller="mainCtrl as main">

<p>{{main.today()}}</p>

</div>

I know, that example had nothing to do with my datepicker sorrows. However, you can see how I changed Angular UI’s datepicker plunker to use the ControllerAs functionality here.

Note: You don’t have to use the ‘this’ keyword. You can instead create an object in your controller, add your properties and methods, and then return that object. The returned object then acts like your scope.

Now, you are loosely coupled and can move your datepicker anywhere within your controller’s hierarchy and can still use its functionality. Sure, you’ll see aliases everywhere, but you won’t ever deal with $parent.$parent.something ever again! In fact, you will know exactly which controller is providing what functionality! Still need to bind events or call $apply on some outside code? No worries, you can always use $scope in your controller for when you need it, rather than, well, all the time.

How does this also prepare you for AngularJS 2.0? Well, as stated before, Angular 2 will no longer have $scope. In fact, controllers are just a part of Angular’s Web Components (or Component Directive, as they are being called), where you simply define your component’s controller as a class that follows the ComponentDirective constructor. You add various class members to ‘this’, and they will be provided to the component’s view. Just like the ControllerAs functionality in 1.1.5+. All in all, ControllerAs is a simple way to attack issues with scope hierarchy as well as get yourself ready for the death of $scope.

Developer Christmas Is Early

In a widely shocking display unlike any that Microsoft has done before, they recently announced that the core of .Net will now be open source!

*Pause for raucous applause*

After years of working with .Net and loving it, I am looking forward to the move to open source. I feel that .Net has an incredible and rich development stack (aside from the presentation layer, I still love HTML/CSS there…) and it will only improve by allowing the massive .Net community to tinker, improve and evolve it.

The move was partially due to having to deal with a lot of issues between their platform and Mono, and partially to allow for development across all platforms. This isn’t Mono’s fault, of course, but when both teams are working to provide a great framework without collaboration, there is bound to be a few issues here and there. In fact, Microsoft reports that they are beginning a collaborative effort with the team at Mono to ensure that the core becomes cross platform.

What interests me in all of this, is just how much this will mean for Windows 10. Windows 10 is to become their next iteration of the pervasive OS that will ship for PC’s, phones, tablets, X-Boxes and apparently, embedded platforms. What better way to support embedded platforms than to make the foundation of your application stack open source!

Along the lines of cross platform and embedded devices, I just have to paraphrase a few of the comments that I have seen surrounding this news…does this mark the beginning of the end of Java? Will we see more and more applications built on a wide array of platforms and devices utilizing .NET? I can only hope, as from what I have seen, C# and .Net are easier to work with, build better user experiences and are built with quite a few newer technologies, practices and patterns in mind.

Furthermore, what will this mean for the web? ASP.NET has already been open sourced alongside the Entity Framework. How will this affect Python, PHP, Coldfusion or even Perl? Companies will soon have the capability to host their old .NET programs on affordable linux servers, where they can be served by ASP.NET over the web with a fresh new look. Who knows, maybe we’ll even see someone calling .NET application code from Python on a linux box. The possibilities are nearly endless! I already love Python, and to be able to use .NET for its strengths alongside Python and it strengths seems like a pretty amazing idea to me!

All in all, I am incredibly excited for this news. While the current core repo on Github is small and doesn’t contain all of .NET’s goodness, do not be alarmed! They are planning on continually moving their code over to the repo. This is the just another amazing step toward a wonderful, more open, more intriguing technological world.

P.S. Visual Studio is now free and fully featured. Huzzah!!!

Delicious and Affordable Cloud Hosting

So recently I spoke with a coworker about trying to find a decent webhost that I could use for various development projects. I have used VMs in the past to create my own servers that I could use for internal testing and hosting. The problem is that if I ever want any of those to be public, I have to jump through a bunch ISP and networking hoops. While I can jump through said hoops…I have no real desire to jump, hop or even skip in that direction.

On top of that, I wanted a host that would allow me to easily work with my servers as if they were my own. I didn’t want a complicated almost IIS-like control panel; I wanted something more akin to actually having my own box…but without the hassle of actually owning said box. I’ve grown accustomed to a certain development lifestyle, you see, and I want to continue living within it. I want hosting to be easy, fully-featured, and of course, I want it to be pretty.

And so it came to pass, that he suggested to me Digital Ocean, a beautiful, simple, and effective place where you can spin up a new server in almost no time. After about 5 minutes of reading their sales pitch, I was already hooked. I signed up and set up my first Ubuntu Docker server in no time. I knew that I had found my hosting paradise.

Here’s a quick rundown of why I love DO, and why you should at least review their site for yourself:

  • Great pricing, on your own dedicated virtual server
  • A full control panel with an amazing user experience
  • Server image backups and snapshots
  • A ridiculous amount of support information
  • A large and passionate community

If any of that sounds good to you, and you’d like a $10 credit for your own DO account, please click here.