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

AngularJS: the Good, the Bad and the Ugly

AngularJS: the Good, the Bad and the Ugly

Looking at Angular with its quirks. Presentation given at JSDay 2014 at Verona (http://2014.jsday.it). Video of the presentation: https://vimeo.com/104093926

Gilles Ruppert

May 14, 2014
Tweet

More Decks by Gilles Ruppert

Other Decks in Technology

Transcript

  1. –Phil Karlton “There are only two hard things in Computer

    Science: cache invalidation and naming things.”
  2. Highlights all singletons service function is instantiated with new factory

    returns the value that is returned provider is like factory/service but can be configured service, factory, and value are all derived from provider. http://stackoverflow.com/questions/15666048/angular-js- service-vs-provider-vs-factory
  3. var app = angular.module(‘examplesApp')! ! app.service('MyService', function () {! this.name

    = '';! this.greeting = function () {! return 'Hello' +! (this.name ? ' ' : ‘') +! this.name + '!';! };! this.setName = function (name) {! this.name = name;! }! });!
  4. var app = angular.module('examplesApp')! ! app.factory('MyFactory', function () {! return

    {! name: '',! greeting: function () {! return 'Hello' +! (this.name ? ' ' : '') +! this.name + '!';! },! setName: function (name) {! this.name = name;! }! };! })
  5. var app = angular.module('examplesApp')! ! app.provider('MyPerson', function () {! var

    name = '';! this.setDefaultName = function (n) {! name = n;! };! ! this.$get = function () {! return {! name: name,! greeting: function () {! return 'Hello' + ! (this.name ? ' ' : '') + this.name + '!';! },! setName: function (name) {! this.name = name;! }! };! };! })! ! app.config(function (MyPersonProvider) {! MyPersonProvider.setDefaultName('Provider');! })
  6. // Actual project code… HELP!!!! angular.module('myApp', [])! .controller(! 'HelloCtrl',! [!

    '$scope',! function ($scope) {! $scope.message =! ! ! ! ! ! 'Where is the normal JS?';! }! ]! );
  7. ! ! app.controller('MainCtrl', function (! $scope,! MyService! $timeout! ) {!

    MyService.setName('Service');! $scope.greeting = MyService.greeting();! ! $timeout(function () {! MyService.setName(‘JSDay 2014’);! $scope.greeting = MyService.greeting();! }, 5000);! ! });
  8. ! ! app.controller('MainCtrl', function (! $scope,! MyService! $timeout! ) {!

    MyService.setName('Service');! $scope.greetingService = MyService;! ! $timeout(function () {! MyService.setName(‘JSDay 2014');! }, 5000);! });
  9. $scope events 2 different directions: going down & up the

    scope tree similar to capture and bubble in regular events why expose different API? leads to $rootScope.$emit(‘JUST_SEND_ME_EVERYWHERE’);
  10. generates functions such as angular.callbacks._0 breaks a lot of APIs

    that don’t handle object notation angular({ JSON_RESPONSE: ‘HERE’ }); alternative: write your own or use jQuery…
  11. Modules implementation no dependency manager no AMD or CJS hard

    to find dependencies which module contains ‘carousel’?
  12. –Misko Hevery “Unfortunately it is way too easy to add

    slow comparison into angular, so it is easy to build slow apps when you don't know what you are doing.” emphasis added by presenter
  13. Where is the M? There is no real ‘model’ ngresource

    helps with loading no convention for modelling your data
  14. https://docs.angularjs.org/guide/ie AngularJS 1.3 is dropping support for IE8 AngularJS 2.0

    ‘modern browsers only (auto-update)’ Chrome, FireFox, Opera, Safari, and IE10/11, modern mobile browsers
  15. Hard to debug point of entry can be hard to

    find difficult to check output with debugger dedicated tool to debug says it all… (batarang) declarative style can be hard with large html files
  16. Teams no conventions no structure no guidelines best practices not

    well defined (yet?) 1 module per file? Per folder? Per app?
  17. Data binding very powerful and easy scopes are in the

    prototype chain complex, auto-updating UIs easy components
  18. // data binding! ! function MainCtrl($scope) {! $scope.isOpen = false;!

    $scope.toggle = function () {! $scope.isOpen = !$scope.isOpen;! };! }! ! ! <div ng-controller="MainCtrl">! <button ng-click="toggle()">Toggle</ button>! <div ng-show="isOpen">! I'm hidden by default!! </div>! </div>
  19. // data binding (simpler)! ! function MainCtrl($scope) {! }! !

    <div ng-controller="MainCtrl">! <button ng-click="isOpen = !isOpen”>! Toggle! </button>! <div ng-show="isOpen">! I'm hidden by default!! </div>! </div>
  20. POJO as view models lean RESTful APIs old legacy APIs

    convoluted structures deep trees very flexible and easy to use
  21. DSL for your app custom elements html macros leads to

    consistency once build, easy to create more pages/widgets
  22. AngularJS 2.0 written in ES6 (transpiled into ES5) better D.I.

    system ES6 module system more modular better performance simpler
  23. – thinkster.io “The learning curve of AngularJS can be described

    as a hockey stick. Initially getting off the ground with apps featuring basic functionality is delightfully easy.”
  24. –thinkster.io “However, when apps eventually grow large or complicated, structure

    without heed to Angular’s inner workings will cause development to become awkward and cumbersome.”
  25. –Blondie “You see, in this world there’s two kinds of

    people, my friend: Those with loaded guns and those who dig. You dig.”