This document provides an overview of key concepts for front end web development using AngularJS, including controllers, data binding, directives, filters, services, routing, and references for further reading. It explains how AngularJS uses controllers to control information on a page through directives like ng-controller. Data binding and services like $http are also covered, along with how to create modules, define views with directives, and configure routing between views.
2. Requirements
oAngularJS has no dependencies on any other libraries or framework.
oAdd `ng-app` attribute in your HTML (angular directive, ng is a short of angular)
oChrome DevTools
4. Controllers
oControl the information that we put/edit on the page.
oTo bring controllers to life.
oDirective called ng-controller.
oHow does angular knows where does that controller live?
oCreating a function that angular will invoke.
oAlways pass the parameter to a controller name $scope which will hold the info.
o$scope is not the model but info we attached to the model
5. Controllers Capabilities
oAngular passes the controller function and it manipulate the $scope
oNotice how the controller never has to communicate with the html.
View
Controller
Model
6. Controllers Capabilities con.
oThis will create a great separation between the model and the view.
ojQuery is different.
oOn larger applications you might need multiple controllers
oDealing with complex objects
oNested controllers.
7. $http Service
oJust an Object that contains methods that I can use.
oEncapsulate HTTP communication (GET, POST, PUT, and DELETE).
oI can use $http inside a controller by asking for it (DI) just by adding another parameter to the
controller function.
oUsing methods like .get, .post
OR
Using an object inside $http.
oBut wait, as it’s asynchronies call, thus it returns a promise (to make sure, use .then on that).
oBEWARE!
oExample: using $http service (GitHub API).
8. Modules
oBuilding a controller in the global namespace is really bad approach (remember!).
oAngular 1.3 and above prevent this by default.
oJust use module please.
oModule pattern, revealing module pattern, and angular module. ( _ ) Mother of modules.⌐■ ■
oThey are different, but they have the same goal, which is to provide a container.
oController usually live in modules (avoiding global namespace).
oWhen do I need a module, when do I use a consisting module?
oCreate a module with a name as first parameter, and array of dependencies of that module.
oRegister your controller in the module (use .controller)
oTell Angular to use your module with `ng-app`.
9. Directives and Views
oBinding directives, model directives, event directives, and display directives.
o`ng-app` and `ng-controller` are directives.
oDirectives to handle a button click for example.
oReminder: Model doesn’t touch the HTML directly.
oUse a binding directive to attach the data to the html (meet `ng-bind`).
oThis called Data Binding Directive.
oData binding moves model data to the view.
o`ng-src` and `ng-href`.
10. Directives and Views con.
oSeparation in concern.
oThe controller and the model focus on the data, while the view only shows those data.
oBut if I want to send information from the user view?
oDirectives allow that also (indirect model view interaction). We have directives that move data
from the view to the model also.
o`ng-model`.
o`ng-click` and `ng-submit`.
12. More Directives
o`ng-show` and `ng-hide` (boolean).
o`ng-include` to include HTML partials.
o`ng-class`, `ng-disable`, `ng-focus`, and `ng-style` (same as they indicate).
o`ng-mouseleave`, `ng-mousemove`, `ng-mouseenter`, and `ng-dblclick` (mouse events).
o`ng-keypress`, `ng-keyup` (keyboard/keypad events).
13. IE Custom Tag Restrictions
oლ(ಠ 益 ಠლ) Y U NO SUPPORT IT?
o4 ways to “directive” (not applied for all directives though. More info on AngularJS website).
oTag name: <ng-*></ng-*>
oAttribute name: <div ng-*=“…”>…</div>, <div ng:*=“…”>…</div>, <div ng_*=“…”>…</div>,
<div data-ng-*=“…”>…</div>, <div x-ng-*=“…”>…</div>
oUse the data-* for HTML validation.
oClass name: <div class=“ng-*”></div>
oHTML Comments.
oPrefer using directives via tag name and attributes over comment and class names.
14. Services
oAngular comes with a lot of services and ability to build your own.
oServices like: sending http requests, computing a hash of a string, saving data into local storage,
manage cookies sent from the server, performing a validation checks… etc.
oController and directives use services.
oBuilt-in services like: $http, $interval, $window, $browser, $anchorScroll, $location, $log,
$timeout, $animate… etc.
oNotice that everything relate to angular (built-in functionality) starts with `$`, like the services
above.
o$log service can be reprogrammed to send information to the server, which will make it easier
to track the problem that the user is having.
15. Services con.
oThere are UI related services.
oWhen you want to force the user to navigate to a new URL.
oPop –up a model dialog to force the user to type data before taking any action.
oBEWARE!
oThe solution to these scenarios is still not to touch UI related objects directly from the model,
but instead using services that can wrap this functionality.
o$location and $anchorScroll
oWhy building your own service? Create reusable logic, create shared data, manage complexity.
16. Routing
oNavigate between views.
oPass parameters between views.
oUse backward and forward (the history of the browser is synchronized with the user)
oWhy? Not putting all the functionality in one view and one controller.
oRouting based on URL.
oRouting engine captures request.
oRouting rules render a response.
oHow does the routing work with angular?
17. Routing con.
oYou have to include another module `ngRoute` (in angular-route.js)
oConfigure rules into $routeProvider (.config).
oUsing .when and .otherwise to assign the URL and the controller for each view.
oSetup a layout view.
o`ng-view` for all.