Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Future-proof AngularJS

Future-proof AngularJS

This talk will show you how to develop an Angular 1.x app in such a way that you'll have an easy transition to Angular 2. By using all features Angular 1 offers that will help you migrate to Angular 2 later on.

This talk was presented at the AngularJS Meetup Berlin.

pascal weiland

September 16, 2015
Tweet

Other Decks in Programming

Transcript

  1. Timeline - vanilla - 2005: Prototype & YUI - 2006:

    Mootools - 2006: jQuery - 2009: node.js - 2009: underscore - 2009: AngularJS (2012: 1.0.0) - 2010: backbone - 2010: knockout - 2011: Ember.js - 2012: RxJS - 2013: react.js - 2015: Angular 2 - 2015: EmberJS 2
  2. Now

  3. Angular 2 - No DDO - No $scope - No

    angular.module - No jqLite - mobile first (offline first) - web components - i18n - sparate di, router, change detection (observables) - server side rendering - new template syntax - Typescript - "classes" - annotations (decorators) - zones
  4. Angular's Getter & Setter angular .module('myApp', []);
 
 angular .module('myApp')

    .controller('Controller', function() {}); 
 angular .module('myApp') .service('DataService', function() {});
  5. $scope & ng-controller - don't use $scope, use this -

    ng-controller="Controller as ctrl" instead - avoid business logic, store only presentation logic - controllers should only be used in routes and directives
  6. Directives - use controller - controller with controllerAs - bindToController

    - name has to contain a - - isolate scope for elements - shared scope for attributes - events in link: scope.$on('$destroy')
  7. Directives angular
 .module('myApp')
 .directive('CakeComponent', CakeComponent)
 
 function CakeComponent() {
 return

    {
 scope: {},
 controller: 'CakeController',
 controllerAs: 'cakeCtrl',
 bindToController: true
 };
 }
  8. Directives angular
 .module('myApp')
 .directive('cakeDirective', cakeDirective);
 
 function cakeDirective() {
 return

    {
 restrict: 'A',
 scope: true,
 controller: function() {},
 controllerAs: 'ctrl',
 bindToController: {
 obj: '='
 }
 };
 }

  9. Service angular
 .module('myApp')
 .service('CakeService', CakeService)
 
 function CakeService() {
 const

    cakeService = this; cakeService.cakes = [];
 cakeService.addCake = (cake) => this.cakes.push(cake) }
  10. Service angular
 .module('myApp')
 .service('CakeService', CakeService)
 
 function CakeService() { let

    cakes = [];
 function addCake(cake) { cakes.push(cake) } const cakeService = {addCake}; return cakeService;
 }
  11. Service angular
 .module('myApp')
 .service('CakeService', CakeService);
 
 class CakeService {
 constructor()

    {
 this.cakes = [];
 }
 addCake(cake) {
 this.cakes.push(cake)
 }
 }
  12. Service angular
 .module('myApp')
 .service('CakeService', CakeService);
 
 const BASE_URL = '';


    class CakeService {
 constructor($http) {
 this.$http = $http;
 }
 getCakes() {
 return this.$http
 .get(BASE_URL)
 .then(res => res.data);
 }
 }
 CakeService.$inject = ['$http'];
  13. ng-init="hello='angular'" don't use ng-init init data via services and resolve

    (routing) or before bootstrapping or via a rendered constant or pass data via directives (&, =, @)
  14. Routing new angular router <ng-viewport> Component Controller $router and append

    $routeConfig to the controller ui router is also good
  15. Structure - common/ - filter.js - components/ - hello/ -

    hello.directive.js - hello.route.js - hello.template.js - hello.controller.js - user/ - user.directive.js - user.template.js - user.controller.js - user.service.js - app.js (bootstraps with each module)
  16. Conclusion - controller-as instead of $scope - everything is a

    directive - Controllers act as View Model (Presenter Logic) - Services act as Model (Business Logic) - TypeScript & ES6 - JSPM / SystemJS / Webpack - Component based folder structure - => easy migration path to Angular2
  17. Links - https://github.com/angular/ngUpgrade - https://github.com/angular-class/NG6-starter - https://github.com/gocardless/angularjs-style-guide - https://github.com/gocardless/es6-angularjs -

    http://addyosmani.com/first/ - http://angularjs.blogspot.de/2015/09/angular-2-survey-results.html - http://angularjs.blogspot.de/2015/08/angular-1-and-angular-2-coexisten - http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-an - http://blog.thoughtram.io/angularjs/es6/2015/01/23/exploring-angular-1. es6.html - http://toddmotto.com/digging-into-angulars-controller-as-syntax/