Skip to content

Instantly share code, notes, and snippets.

@btford
Created October 30, 2014 06:59
Show Gist options
  • Save btford/d7be6f075469e16b11ea to your computer and use it in GitHub Desktop.
Save btford/d7be6f075469e16b11ea to your computer and use it in GitHub Desktop.
Why you shouldn't worry so much about migrating with Angular

Several developers asked me about how difficult it will be to migrate Angular 1 to Angular 2. Angular 2 isn't done, so I legitimately have no idea how hard it will be. But there are a few high-level guiding principals in the design of Angular 1 that make adapting to changes like this fairly painless.

Angular 1 was designed so it would have a fairly minimal API surface. Let's look at controllers, since these are the meat of your app. Controllers are just functions that get passed other components as arguments:

MyController ($scope) {
  $scope.list = [];
  
  $scope.addItem = function (name) {
    $scope.list.push({
      name: name
      done: false
    });
  };
}

The way you integrate this into Angular app is to register it with a module:

angular.module('myModule', []).controller('MyController', MyController);

But you could also call the function MyController yourself, then hook up events:

var scope = {};
MyController(scope);
someDom.addEventListener('click', function () {
  scope.addItem(myForm.value);
  renderList(); // defined elsewhere
});

Even though Angular 2 will probably not have an angular.module API, it should be easy to adapt much of your code to the new API.

Some frameworks rely on inheriting from specific classes for their models. If you subclass these framework-provided classes, your classes will break whenever the API of the superclass changes. This is a technique that Angular specifically avoids. In Angular, you can use POJOs as models, or you can build up your own classes. Either way, changes in Angular itself will not affect your model's code.

Angular has other such features that reduce needless coupling, but these are just two examples.

@FlipOne
Copy link

FlipOne commented Nov 12, 2014

It seems to me that AngularJS 2.0 --- despite some of the not-so-unreasonable complaints and apprehensions --- is a developer-focused and forward-looking effort meant to "correct" the architectural "mistakes" (humbly acknowledged by its creator) of the current version. I believe the gains will far outweigh the pains of 1.x to 2.0 conversion.

I hope, though, that the AngularJS Team will provide clear guidance and documentation in two areas:

  1. How to architect, structure and code AngularJS applications FROM HEREON, so it will be much easier/simpler for us to convert to 2.0 when it's ready.
  2. How to do the 1.x to 2.0 conversion - scenarios, code samples, articles, step-by-step tutorials

Despite the breaking changes, I'm really excited about v2.0(!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment