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

Intro to AngularJS (JakartaJS Dec 2013)

Intro to AngularJS (JakartaJS Dec 2013)

Giovanni Sakti

December 18, 2013

Transcript

59 slides
  1. Angular JS is a client-side, JS MVC framework for creating

    rich, “single page application” A.K.A SPA
  2. $(“#change-­‐me”).html(“I  am  changed!”) Hey “change-me” you should turn yourself into

    “I am changed” <div  id=‘change-­‐me’>     I  will  not  yield!   </div> <div  id=‘change-­‐me’>     I  am  changed!   </div>
  3. $scope.secretData=‘I  am  the  guardian  of  a  secret   data’ I

    must follow whatever ‘secretData’ variable has in store <div  id=‘change-­‐me’>     {{secretData}}   </div> <div  id=‘change-­‐me’>     I  am  the  guardian  of  a  secret  data   </div>
  4. $scope.secretData=‘JakartaJS  will  be  held  at  Dec,   17th’ <div  id=‘change-­‐me’

     ng-­‐model=‘secretData’>     JakartaJS  will  be  held  at  Dec,  17th   </div>
  5.    .directive('activeLink',  ['$location',  function(location)  {          return

     {              restrict:  'A',              link:  function(scope,  element,  attrs)  {   !                var  path  =  attrs.ngHref;   !                //hack  because  path  does  not  return  including  hashbang                  path  =  path.substring(1);                  if  (path  ===  '')  {  path  =  '/';  }   !                scope.location  =  location;                  scope.$watch('location.path()',  function(newPath)  {                      if  (path  ===  newPath)  {                          element.parent().addClass('active');                      }  else  {                          element.parent().removeClass('active');                      }                  });              }          };      }]); "only do DOM manipulation in a directive if and only if absolutely necessary” -a wise man
  6. rootScope scope scope scope scope <html>     <head></head>  

      <body  ng-­‐app=“jakartajs”>       <div  ng-­‐controller=“first”>       </div>       <div  ng-­‐controller=“second”>         <tree  treeData=“data”>         <input  ng-­‐model=“name”>       </div>     </body>   </html>
  7.    .directive('activeLink',  ['$location',  function(location)  {          return

     {              restrict:  'A',              link:  function(scope,  element,  attrs)  {   !                var  path  =  attrs.ngHref;   !                //hack  because  path  does  not  return  including  hashbang                  path  =  path.substring(1);                  if  (path  ===  '')  {  path  =  '/';  }   !                scope.location  =  location;                  scope.$watch('location.path()',  function(newPath)  {                      if  (path  ===  newPath)  {                          element.parent().addClass('active');                      }  else  {                          element.parent().removeClass('active');                      }                  });              }          };      }]);
  8. User.get({id:  1})     .$then(function(response)  {       console.log(response)

        },  function(error)  {       console.log(error)     }); User.save({id:  1},  {first_name:  “Giovanni”})     .$then(function(response)  {       console.log(response)     },  function(error)  {       console.log(error)     }); query,  get,  new,  create,  edit,  update,  delete   7  default  action,  can  be  added  if  necessary
  9.    .config(['$routeProvider',  function  ($routeProvider)  {          $routeProvider

                 .when('/',  {                  templateUrl:  'views/main.html',                  controller:  'MainCtrl'              })              .when('/databinding',  {                  templateUrl:  'views/databinding.html',                  controller:  'DatabindingCtrl'              })              .when('/directive',  {                  templateUrl:  'views/directive.html',                  controller:  'DirectiveCtrl'              })              .otherwise({                  redirectTo:  '/'              });      }]);
  10. npm  install  -­‐g  yo  grunt-­‐cli  bower   npm  install  -­‐g

     generator-­‐angular   yo  angular *assumming you have installed npm
  11. app/   node_modules/   test/   .bowerrc   .editorconfig  

    .gitattributes   .gitignore   .jshintrc   Gruntfile.js   bower.json   karma-­‐e2e.conf.js   karma.conf.js   package.json  
  12. app/     bower_components/     images/     scripts/

          configs/       controllers/       directives/       resources/       services/       app.js     styles/     views/     404.html     favicon.ico     index.html     robots.txt  
  13. test/     spec/       configs/    

      controllers/         main.js       directives/       resources/       services/   karma-­‐e2e.conf.js   karma.conf.js  
  14. We must remove the urge to write code imperatively. !

    e.g. jQuery usage should be replaced if possible by databinding and directive.
  15. 1. Authentication should be done remotely to receive ‘token’ !

    2. ‘token’ is sent in every request for authenticating our users ! 3. Authorization must be done both in API and your AngularJS app

More Decks by Giovanni Sakti

Other Decks in Programming