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

Taking the leap of faith into AngularJS

Taking the leap of faith into AngularJS

Talk about AngularJS, 101, introduction and best practices prepared for GDG Dev Fest Barcelona, 2013.

PatrickHeneise

October 11, 2013
Tweet

More Decks by PatrickHeneise

Other Decks in Technology

Transcript

  1. Patrick Heneise ‣ BSc Computer Science in Media ‣ MSc

    Media Technology ‣ Information Architect, Consultant ‣ JavaScript Enthusiast Friday 11 October 13
  2. ‣ Data Binding ‣ View-Controller Architecture ‣ Deep Linking ‣

    Form Validation ‣ Directives ‣ Localization ‣ Injections ‣ Tests ‣ Validations AngularJS Features Friday 11 October 13
  3. ‣ Templates ‣ Filters ‣ Data Binding ‣ Links &

    Images ‣ Routing ‣ Views ‣ Event Handlers ‣ Services ‣ Animations Friday 11 October 13
  4. . ├── Gruntfile.js ├── app │ ├── 404.html │ ├──

    favicon.ico │ ├── images │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ ├── index.html │ ├── robots.txt │ ├── scripts │ │ ├── app.js │ │ └── controllers │ │ └── main.js │ ├── styles │ │ └── main.scss │ └── views │ └── main.html ├── bower.json ├── karma-e2e.conf.js ├── karma.conf.js ├── package.json └── test ├── runner.html └── spec └── controllers └── main.js Friday 11 October 13
  5. First Steps to a ‘real’ App ‣ Install: ‣ Angular-UI

    ‣ Angular-UI-Router ‣ Angular-UI-Bootstrap ‣ SASS Bootstrap 3 Friday 11 October 13
  6. .config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/junks'); $stateProvider .state('junks', { url: '/junks', templateUrl:

    'views/junk/index.html', controller: 'JunkIndexCtrl' }) }) .run(['$state', function ($state) { $state.transitionTo('junks'); }]); Friday 11 October 13
  7. ‣ A state corresponds to a "place" in the application

    ‣ A state describes what the UI looks like and does at that place. ‣ States often have things in common Friday 11 October 13
  8. Factory myApp.factory('helloWorldFromFactory', function() { return { sayHello: function() { return

    "Hello, World!" } }; }); http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/ Friday 11 October 13
  9. api.js angular.module('Junkagular', []) & .factory('$api', function ($http) { & &

    var apiRoot = 'http://127.0.0.1/api/', & & & apiTimeout = 30000, & & & api = { & & & & config: { & & & & & apiRoot: apiRoot & & & & }, & & & & junk: {} & & & }; Friday 11 October 13
  10. api.junk.index = function (callback) { $http.defaults.headers.common.apikey = '123'; $http.get(apiRoot +

    'junks', { headers: {'Content-Type': 'application/json'} }).then(function (response) { callback(null, response.data); }); }; Friday 11 October 13
  11. .controller('JunkNewFormCtrl', function ($scope, $location, $api) { $scope.submit = function ()

    { $api.junk.create($scope.junk, function (error, data) { $scope.junk = data; $location.path('/'); }); }; }) Controller Friday 11 October 13
  12. View <form class="form-horizontal" role="form" ng-controller="JunkNewFormCtrl"> <div class="form-group"> <label for="text" class="col-lg-2

    control-label">Text</label> <div class="col-lg-10"> <input type="text" class="form-control" id="text" name="text" ng-model="junk.text" placeholder="Text" required> </div> </div> <div class="form-group"> <label for="url" class="col-lg-2 control-label">Url</label> <div class="col-lg-10"> <input type="text" class="form-control" id="url" placeholder="Url" name="url" ng-model="junk.url" required> </div> </div> <div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <button type="submit" class="btn btn-default" ng-click="submit()">Create Junk</button> </div> </div> </form> Friday 11 October 13
  13. <form class="form-horizontal" role="form" ng-controller="JunkNewFormCtrl"> <div class="form-group"> <label for="text" class="col-lg-2 control-label">Text</label>

    <div class="col-lg-10"> <input type="text" class="form-control" id="text" name="text" ng-model="junk.text" placeholder="Text" required> </div> </div> <div class="form-group"> <label for="url" class="col-lg-2 control-label">Url</label> <div class="col-lg-10"> <input type="text" class="form-control" id="url" placeholder="Url" name="url" ng-model="junk.url" required> </div> </div> <div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <button type="submit" class="btn btn-default" ng-click="submit()">Create Junk</button> </div> </div> </form> Friday 11 October 13
  14. <script type="text/javascript"> $(function() { var textToHide = $('p').text().substring(100); var visibleText

    = $('p').text().substring(1, 100); $('p') .html(visibleText + ('<span>' + textToHide + '</span>')) .append('<a id="read-more" title="Read More" style="display: block; cursor: pointer;">Read More&hellip;</a>') .click(function() { $(this).find('span').toggle(); $(this).find('a:last').hide(); }); $('p span').hide(); }); </script> jQuery function to create an excerpt Friday 11 October 13
  15. Angular Filter to create an excerpt .filter('excerpt', function () {

    return function (input) { return input.substr(0, input.lastIndexOf(' ', 200)) + '...'; }; }) View: {{description | excerpt}} Friday 11 October 13
  16. Directives .directive('whenScrolled', function () { return function(scope, element, attr) {

    var raw = element[0]; element.bind('scroll', function() { if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) { scope.$apply(attr.whenScrolled); } }); }; }); <div id="items" when-scrolled="loadMore()"> Friday 11 October 13
  17. ‣ What is AngularJS? ‣ What are the strengths of

    AngularJS? ‣ How to setup a project with Grunt/ Yeoman/Bower? ‣ How to create routes, controllers and views? ‣ What are directives and filters? Friday 11 October 13
  18. Unit vs. E2E Tests ‣ Test small isolated units ‣

    Can be tested from every angle ‣ Testing full areas of the application ‣ Entire Stack ‣ ‘Robot using your App’ Friday 11 October 13
  19. describe('Controller: JunkIndex', function () { beforeEach(function() { browser().navigateTo('/junks'); pause(); });

    it('should attach a list of junk to the scope', function () { expect(repeater('a.junk').count()).toBe(4); }); }); Friday 11 October 13
  20. ‣ What is the difference between module.factory and module.service and

    how might both be applied? - https://groups.google.com/forum/#!msg/angular/56sdORWEoqg/HuZsOsMvKv4J ‣ Service, Factory and Provider in Code - http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/ ‣ Understanding Angular Services - http://docs.angularjs.org/guide/ dev_guide.services.understanding_services ‣ Advanced Testing and Debugging in AngularJS - http://www.yearofmoo.com/2013/09/ advanced-testing-and-debugging-in-angularjs.html ‣ Full-Spectrum Testing with AngularJS and Karma - http://www.yearofmoo.com/2013/01/full- spectrum-testing-with-angularjs-and-karma.html ‣ Angular Directives - http://docs.angularjs.org/guide/directive ‣ The basics of using ui-router with AngularJS - http://joelhooks.com/blog/2013/07/22/the-basics- of-using-ui-router-with-angularjs/ Friday 11 October 13