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.

@ndee
Copy link

ndee commented Oct 30, 2014

It's definitely the non invasiveness of angular which let's me sleep in the night. I'm not really concerned about the lack of controllers and modules in ng2.0 but what makes me a little bit nervous are the transformation of directives to "components". We have lots of directives with complex link functions .What happens with those?

@0x-r4bbit
Copy link

@ndee these are prolly gonna be components with complex functionality.

@arvidkahl
Copy link

I feel like modularizing your controllers and directives as much as you can while moving the logic into services is the way to go. This way, anything in controllers and directives will be more easily migrated, likely through automated tooling in most cases.

@benc
Copy link

benc commented Oct 30, 2014

What about the new templating language? That would be a lot harder to change.

@brettstack
Copy link

I think the reactions so far have been a little over the top. My biggest gripe is with the new HTML syntax. While there may have been very good reasons for changing it (performance?), you can't deny that it is far more complex. Although I would honestly love to be convinced otherwise. Perhaps as documentation starts to appear it won't seem as scary.

@kaanozcan
Copy link

I am little bit concerned with html, controllers, directives and scope too but I think you cant progress without pain. Just decide if you would want a framework hacked his way around to the top of its potential in 5 years or completely rewritten itself to a point much better than that in a much shorter time. It seems to me that modifying a product this way is not a decision of a business analyst it is a decision of a developer and a well taken one considering ES6. I would do the same if i were them. Good thing is there will be no dept in my code after all this is over. ;)

@huug
Copy link

huug commented Oct 31, 2014

I guess it will not tot take long before convertors for the templates will pop up like html2haml and so on...

@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