Powerful Applications with AngularJS

Powerful Applications with AngularJS

B3b2139e4f2c0eca4efe2379fcebc1c5?s=128

Anna Filina

May 30, 2015
Tweet

Transcript

  1. foolab.ca | @foolabca Powerful Applications with AngularJS CakeFest, New York

    - May 30, 2015
  2. Anna Filina • Developer • Problem solver • Coach •

    Advisor 2
  3. Objectives • More advanced front-end. • Maintainable code with fewer

    bugs. • AngularJS site in 30 minutes. • Inspire to build great apps. 3
  4. The problem with JS • Messy content updates. • HTML

    tangled with JS. • Hard to keep everything in sync. 4
  5. AngularJS • Automatic content updates. • HTML separated from JS

    logic. • Everything automatically in sync. 5
  6. Data flow 7 products selectedProduct

  7. HTML <html ng-app> <head> <script src="angular.js"></script> <script src="app.js"></script> </head> <body>

    <div ng-controller="ProductsCtrl"> </div> </body> </html> 8
  8. Inside the DIV <div ng-controller="ProductsCtrl"> <div ng-repeat="item in products"> <img

    ng-src="{{ item.image }}"> <p>{{ item.name }}</p> </div> </div> 9
  9. Details <div ng-show="selectedProduct"> <h2>{{ selectedProduct.name }}</h2> </div> [...] <div ng-repeat="item

    in products"> <a href="" ng-click="showDetails(item)"></a> </div> 10
  10. Javascript function ProductsCtrl($scope) { $scope.products = [ { "id": "product-1",

    "name": "Skyrim", "description": "Short product description.", "image": "http:...", "price": 19.99, "stock": 10 }]; $scope.showDetails = function(item) { $scope.selectedProduct = item; }; }; 11
  11. Data flow 12 $scope.products $scope.showDetails $scope.selectedProduct <div ng-repeat="item in products">

    <a ng-click="showDetails(item)"> <h2>{{ selectedProduct.name }}</h2>
  12. Directives (ng-) 13 ng-show="selectedProduct" ng-class="{nostock: selectedProduct.stock == 0 }"

  13. Data flow 15 maxPrice updateResults

  14. HTML <input ng-model="minPrice" ng-change="updateResults()"> <div ng-repeat="item in products" ng-class="{dim: item.hidden

    == true}"> 16
  15. Javascript $scope.query = ''; $scope.minPrice = 1; $scope.maxPrice = 20;

    $scope.updateResults = function() { for (var i = 0; i < $scope.products.length; i++) { var product = $scope.products[i]; product.hidden = true; [...] if (matchesQuery && matchesPrice) { product.hidden = false; } }; } 17
  16. Directives (ng-) 18 <input ng-model="maxPrice" ng-change="updateResults()"> $scope.maxPrice $scope.updateResults = function()

    {} <div ng-class="{dim: item.hidden == true}">
  17. HTML <html ng-app="myApp"> [...] <script src="angular.js"></script> <script src="angular-resource.js"></script> 20

  18. Javascript var app = angular.module('myApp', ['ngResource']); app.controller('ProductsCtrl', function($scope, $resource) {

    var url = 'http://api.shop.dev/v1.0/products'; $resource(url).get({}, function(response) { $scope.products = response.data; }); }); 21
  19. None
  20. Services • Development: PHP, JS, etc. • Fix problems: bugs,

    performance, etc. • Workshops: AngularJS, testing, Symfony, etc. • Advisor: testing strategy, architecture, etc. 25
  21. @afilina afilina.com