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

AngularJS 1.3.0 - let's talk about changes! ngP...

AngularJS 1.3.0 - let's talk about changes! ngPoznan

This presentation covers:
* AngularJS roadmap
* 1.3.0 breaking changes
* Performance improvements
* New features

This talk was given during the ngPoznan meeting in Poznań, Poland, on November 20th, 2014. *Video will be uploaded soon*

Some code examples: https://typeofweb.github.io/angular-comparison/

Michał Miszczyszyn

November 20, 2014
Tweet

More Decks by Michał Miszczyszyn

Other Decks in Programming

Transcript

  1. no IE8 support! • Won’t break the second you update,

    but… it might • Tests are not run on IE8 • Smaller codebase, less hacks
  2. falsy values • Before, some strings were falsy: 
 'f',

    '0', 'false', 'no', 'n', '[]' • Before, empty array was falsy: [] • Now only JS falsy values are
  3. falsy values • Before 1.3 this would display nothing: $scope.test

    = 'no'; <div ng-if="test">Nope.</div> • Have we ever done it?
  4. falsy values • But we did rely on this: $scope.empty

    = []; <div ng-if="empty">Nope.</div> • Removed in 1.3 as well
  5. stateless filters • Won't update if injected data changes •

    Huuuge performance gain - caching • filter.$stateful = true
  6. benchpress • Macro benchmarking tool • Performance of real applications

    • Measures garbage collector too • On GitHub by Angular team
  7. benchpress • AngularJS 1.3 compared to 1.2: • DOM manipulation:


    4.4x faster, 73% less garbage • digest:
 3.5x faster, 87% less garbage
  8. benchpress • The main point is: • Don't worry about

    Angular's overhead, worry about your code's performance!
  9. production mode • There's a lot of debug info everywhere

    • Some tools (eg. Protractor) use them but they're not needed in production
  10. $httpProvider.useApplyAsync(true); • 1.2: every resolved request = digest • You

    can now configure it • If many responses come in a short time, only one digest will be called
  11. $rootScope.$applyAsync() • You can use this in your own modules

    • It queues up multiple apply calls
 within about 10ms
  12. Material Design • It's a styleguide from Google • Angular-material-design

    is a set of lightweight UI elements based on it • Requires angular 1.3
  13. angular-material-design • Super easy to use • Built with flexbox

    • Integration with ionic is coming… • material.angularjs.org
  14. ngAria • Talking about accessibility… • This new module adds

    ARIA attributes • aria-* • checked, disabled, hidden, invalid…
  15. ngStrictDi • <x ng-app="…" ng-strict-di> • Fails on functions without

    annotation • Incompatible with Batarang
 :(
  16. $scope.$watchGroup() • Takes an array and a callback • Watch

    many properties • Callback - when any of them changes
  17. Forms • Forms in Angular are cool, right? • But

    yet some things were missing…
  18. ngModelOptions • Usecase • Update model value after: • User

    focuses out of the input • A given time after the last keystroke
  19. ngModelOptions <input ng-model="value1" ng-model-options= "{ updateOn: 'blur' }"> {{value1}} •

    Angular 1.3: same length;
 more clear, no additional variables
  20. ngModelOptions • Controller: var timer; $scope.onBlur = function () {

    $timeout.cancel(timer); $scope.value2Final= $scope.value2;
 };
  21. ngModelOptions • Controller: $scope.$watch('value2', function () { $timeout.cancel(timer); timer =

    $timeout(function () { $scope.value2Final = $scope.value2; }, 1000);
 });
  22. ngModelOptions • Angular 1.3 is all declarative: <input ng-model="value2" ng-model-options="{

    updateOn: 'default blur', debounce: { default: 1000, blur: 0 }
 }">
  23. form validation • All HTML5 validation attributes are now properly

    bound to ngModel • No need for ng-pattern, ng-minlength, ng-maxlength, although they are still available
  24. form validation • There are differences though: • <input maxlength="13">


    Some browsers disallow any text longer than 13 characters • <input ng-maxlength="13">
 Only Angular error
  25. custom validators • In Angular 1.2 - $parsers and $formatters

    pipelines • Had to call $setValidity manually • In Angular 1.3 there's a separate pipeline for that
  26. custom validators • How? custom directive with
 require: 'ngModel' •

    Add a function on object
 ngModel.$validators
  27. custom validators require: 'ngModel', link: function (_, _, _, ngModel)

    { ngModel.$validators.three = function (value) { return (value === 3);
 };
 }
  28. custom async validators • Long awaited feature! • As simple

    as the validators pipeline • How? Add a function returning a promise to the pipeline
  29. custom async validators require: 'ngModel', link: function (_, _, _,

    ngModel) { ngModel.$asyncValidators.async = function (value) { return promise; }; }
  30. custom async validators • $pending property on the controller while

    validation is in progress • Async validators are called after all other validators pass
  31. $touched • One more small thing • $touched property added

    next to $dirty, $pristine etc… • It's set to true after user focuses out of an input
  32. $touched • We had to simulate that in Angular 1.1

    and 1.2 way too many times • This tiny little property makes me personally very happy! :)
  33. improved SVG support • ng-repeat inside of SVG now works

    • You can use templateNamespace: 'svg'
 to create SVG directives
  34. improved ng-src support • "/ngPoznan/{{id}}/a.jpg" • Invalid request if id

    is empty • Won't happen anymore. • Fixed in ng-src, ng-href, ng-srcset
  35. improved animations • Added ng-animate-children • Added staggering animations •

    Meaningful transitions (WiP) • But that's a topic for another presentation…
  36. one-time binding • New syntax introduced • Expressions that start

    with :: are one‑time expressions • One-time expressions will not be updated after they are non-undefined
  37. one-time binding • It's good for performance - less watches

    • Can be used everywhere:
 {{::val}} <x dir="::val">
  38. one-time binding • It should be possible to define own

    syntax for expressions, right? • Well… it is!
  39. (function() {
 var wrap,
 __slice = [].slice;
 
 wrap =

    function(listener, toDo, invokeAlways, isDefined) {
 return _.wrap(toDo, function() {
 var args, originalFn, value, valueIsDefined;
 originalFn = arguments[0], value = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
 valueIsDefined = isDefined(value);
 if (!invokeAlways && !valueIsDefined) {
 return;
 }
 originalFn.call.apply(originalFn, [this, value].concat(__slice.call(args)));
 if (valueIsDefined) {
 return listener.cleanup();
 }
 });
 };
 
 angular.module('ngWatchWhen', []).constant('ngWatchWhenRegEx', /^\:\:([^\:]+)(\:\:[^\:]+)$/).value('ngWatchWhenCloneExpression', function(getter) {
 var clone;
 clone = function() {
 return getter.apply(this, arguments);
 };
 return _.merge(clone, getter);
 }).config(function($provide) {
 return $provide.decorator('$parse', function($delegate, ngWatchWhenRegEx, ngWatchWhenDelegateFactory, ngWatchWhenCloneExpression) {
 return _.wrap($delegate, function($parse, exp, interceptor) {
 var args, match, onceExp, onceStr, whenExp, whenStr;
 if (angular.isString(exp) && (match = exp.match(ngWatchWhenRegEx))) {
 exp = match[0], whenStr = match[1], onceStr = match[2];
 whenExp = $parse.apply(this, [whenStr]);
 onceExp = ngWatchWhenCloneExpression($parse.apply(this, [onceStr, interceptor]));
 onceExp.$$watchDelegate = ngWatchWhenDelegateFactory(whenExp, onceExp);
 return onceExp;
 } else {
 args = [exp];
 if (interceptor != null) {
 args.push(interceptor);
 }
 return $parse.apply(this, args);
 }
 });
 });
 }).value('ngWatchWhenDelegateFactory', function(whenExp, onceExp) {
 return _.wrap(onceExp.$$watchDelegate, function(originalWatchDelegate, scope, listener, objectEquality, parsedExpression) {
 var onceWatch, registerOnceWatch, whenWatch;
 onceWatch = null;
 registerOnceWatch = function() {
 onceWatch = {};
 onceWatch.deregister = originalWatchDelegate.call(this, scope, listener, objectEquality, parsedExpression);
 return onceWatch.watcher = _.first(scope.$$watchers);
 };
 registerOnceWatch();
 whenWatch = scope.$watch(whenExp, function() {
 if (!_.contains(scope.$$watchers, onceWatch.watcher)) {
 return registerOnceWatch();
 }
 });
 return function() {
 whenWatch();
 if (_.contains(scope.$$watchers, onceWatch.watcher)) {
 return onceWatch.deregister();
 }
 };
 });
 });
 
 }).call(this);
  40. one-time binding • But seriously, it's quite complicated. • This

    simple function was 69 LoC • There are already two (or more?) modules on GitHub doing that!
  41. AngularJS 1.3 • No IE8 support • Huge performance improvements

    • Better accessibility (ngAria) • Material Design
  42. AngularJS 1.3 • ngModelOptions • Boosted forms and validators (async!)

    • Improved SVG • Animations on steroids (WiP) • Added one-time binding
  43. • Don't worry! • Angular 1.3 will be supported for

    two more years after the release of 2.0! “I heard about AngularJS 2.0…”
  44. • 1.3 branch has the new maintainer already: Pete Bacon

    Darwin took over • Angular creators promised to release a migration guide from 1.x to 2.0 “I heard about AngularJS 2.0…”