foolab.ca | @foolabcaPowerful Applicationswith AngularJSCakeFest, New York - May 30, 2015
View Slide
Anna Filina• Developer• Problem solver• Coach• Advisor2
Objectives• More advanced front-end.• Maintainable code with fewerbugs.• AngularJS site in 30 minutes.• Inspire to build great apps.3
The problem with JS• Messy content updates.• HTML tangled with JS.• Hard to keep everything in sync.4
AngularJS• Automatic content updates.• HTML separated from JS logic.• Everything automatically in sync.5
Data flow7products selectedProduct
HTML8
Inside the DIV{{ item.name }}9
Details{{ selectedProduct.name }}[...]10
Javascriptfunction 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
Data flow12$scope.products$scope.showDetails$scope.selectedProduct{{ selectedProduct.name }}
Directives (ng-)13ng-show="selectedProduct"ng-class="{nostock: selectedProduct.stock == 0 }"
Data flow15maxPrice updateResults
HTMLng-model="minPrice"ng-change="updateResults()">ng-repeat="item in products"ng-class="{dim: item.hidden == true}">16
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
Directives (ng-)18ng-model="maxPrice"ng-change="updateResults()"> $scope.maxPrice$scope.updateResults = function() {}
HTML[...]20
Javascriptvar 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
Services• Development: PHP, JS, etc.• Fix problems: bugs, performance, etc.• Workshops: AngularJS, testing, Symfony, etc.• Advisor: testing strategy, architecture, etc.25
@afilina afilina.com