UP AND RUNNING WITH FRONT END WEB DEVELOPMENT ANGULARJS
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
Angular Architecture oTwo Way Data Binding oDirty Checking oDependency Injection (remember)
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
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
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.
$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).
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`.
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`.
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`.
Filters oBasic format: expression | filterName:parameter o`ng-repeat` to loop. oSome useful filters: currency, date, filter, limitTo, lowercase, uppercase, number, orderBy, and json (for debugging).
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).
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.
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.
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.
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?
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.
References oAngularJS (https://angularjs.org/) oData Binding – AngularJs (https://docs.angularjs.org/guide/databinding) oControllers – AngularJS (https://docs.angularjs.org/guide/controller) o$http – AngularJS (https://docs.angularjs.org/api/ng/service/$http) oModules – AngularJS (https://docs.angularjs.org/guide/module) oDirectives – AngularJS (https://docs.angularjs.org/api/ng/directive) oFilters – AngularJS (https://docs.angularjs.org/guide/filter) oServices – AngularJS (https://docs.angularjs.org/guide/services) ongRoute – AngularJS (https://docs.angularjs.org/api/ngRoute)

Introduction to AngularJS

  • 1.
    UP AND RUNNING WITHFRONT END WEB DEVELOPMENT ANGULARJS
  • 2.
    Requirements oAngularJS has nodependencies on any other libraries or framework. oAdd `ng-app` attribute in your HTML (angular directive, ng is a short of angular) oChrome DevTools
  • 3.
    Angular Architecture oTwo WayData Binding oDirty Checking oDependency Injection (remember)
  • 4.
    Controllers oControl the informationthat 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 passesthe 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. oThiswill 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 anObject 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 controllerin 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 oBindingdirectives, 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 Viewscon. 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`.
  • 11.
    Filters oBasic format: expression| filterName:parameter o`ng-repeat` to loop. oSome useful filters: currency, date, filter, limitTo, lowercase, uppercase, number, orderBy, and json (for debugging).
  • 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 TagRestrictions 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 witha 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 areUI 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. oPassparameters 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 haveto 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.
  • 18.
    References oAngularJS (https://angularjs.org/) oData Binding– AngularJs (https://docs.angularjs.org/guide/databinding) oControllers – AngularJS (https://docs.angularjs.org/guide/controller) o$http – AngularJS (https://docs.angularjs.org/api/ng/service/$http) oModules – AngularJS (https://docs.angularjs.org/guide/module) oDirectives – AngularJS (https://docs.angularjs.org/api/ng/directive) oFilters – AngularJS (https://docs.angularjs.org/guide/filter) oServices – AngularJS (https://docs.angularjs.org/guide/services) ongRoute – AngularJS (https://docs.angularjs.org/api/ngRoute)

Editor's Notes

  • #3 **Simple Example** &amp;lt;body ng-app&amp;gt; {{1+2}} &amp;lt;/body&amp;gt; =&amp;gt; &amp;lt;div ng-app&amp;gt;&amp;lt;/div&amp;gt;
  • #5 **Main View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; {{message}} &amp;lt;/div&amp;gt; **Controller** var MainController = function($scope) { $scope.message = &amp;apos;Hello World!&amp;apos;; }
  • #7 **Controller** var MainController = function($scope) { var person = { fname: &amp;apos;Anas&amp;apos;, lname: &amp;apos;Shekhamis&amp;apos;, img: &amp;apos;https://pbs.twimg.com/profile_images/461250252507328512/a8L2fwnk.png&amp;apos; } $scope.person = person; $scope.message = &amp;apos;Hello World!&amp;apos;; } **View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; {{message}} Name is: {{person.fname}} {{person.lname}} &amp;lt;br&amp;gt; &amp;lt;img ng-src=&amp;quot;{{person.img}}&amp;quot; alt=&amp;quot;{{person.fname}} {{person.lname}}&amp;quot; title=&amp;quot;{{person.fname}} {{person.lname}}&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt;
  • #8 **Controller** var MainController = function($scope, $http) { var onUserComplete = function (response) { $scope.user = response.data; }, onError = function(reason) { $scope.error = &amp;quot;Could not fetch the user!&amp;quot;; } $http.get(&amp;quot;https://api.github.com/users/angular&amp;quot;) .then(onUserComplete, onError); } **View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; Name: {{user.name}} &amp;lt;br&amp;gt; Link on GitHub: &amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt;
  • #9 **Module** var github = angular.module(‘githubAPI&amp;apos;, []); **Controller** var MainController = function($scope, $http) { var onUserComplete = function (response) { $scope.user = response.data; }, onError = function(reason) { $scope.error = &amp;quot;Could not fetch the user!&amp;quot;; } $http.get(&amp;quot;https://api.github.com/users/angular&amp;quot;) .then(onUserComplete, onError); } **Assigning the controller** github.controller(&amp;apos;MainController&amp;apos;, MainController); **View** &amp;lt;div ng-app=“githubAPI&amp;quot;&amp;gt; &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; Name: {{user.name}} &amp;lt;br&amp;gt; Link on GitHub: &amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;
  • #11 **View (example 1)** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;form name=&amp;quot;searchUser&amp;quot;&amp;gt; Search User:&amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt;&amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; Name: {{user.name}} &amp;lt;br&amp;gt; Link on GitHub: &amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt; **View (example 2)** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;form name=&amp;quot;searchUser&amp;quot;&amp;gt; Search User:&amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt;&amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot; ng-click=&amp;quot;search(username)&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; Name: {{user.name}} &amp;lt;br&amp;gt; Link on GitHub: &amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt; **View (example 3)** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User:&amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt;&amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; Name: {{user.name}} &amp;lt;br&amp;gt; Link on GitHub: &amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt;
  • #12 **Controller** var MainController = function($scope, $http) { var onUserComplete = function (response) { $scope.user = response.data; $http.get($scope.user.repos_url) .then(onRepos, onError); }, onError = function(reason) { $scope.error = &amp;quot;Could not fetch the data!&amp;quot;; }, onRepos = function(response) { $scope.repos = response.data; }; $scope.username = &amp;apos;angular&amp;apos;; $scope.reposSort = &amp;quot;-stargazers_count&amp;quot;; $scope.search = function(username) { $http.get(&amp;quot;https://api.github.com/users/&amp;quot; + username) .then(onUserComplete, onError); } } **View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User:&amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt;&amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;h1&amp;gt;&amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt; (created at: {{user.created_at | date:&amp;apos;short&amp;apos;}})&amp;lt;/h1&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;br&amp;gt; &amp;lt;h3&amp;gt;Repos&amp;lt;/h3&amp;gt; &amp;lt;input type=&amp;quot;search&amp;quot; placeholder=&amp;quot;search by repo name&amp;quot; ng-model=&amp;quot;searchTerm&amp;quot; /&amp;gt; Order: &amp;lt;select ng-model=&amp;quot;reposSort&amp;quot;&amp;gt; &amp;lt;option value=&amp;quot;+name&amp;quot;&amp;gt;Name&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;-stargazers_count&amp;quot;&amp;gt;Stars&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;+language&amp;quot;&amp;gt;Language&amp;lt;/option&amp;gt; &amp;lt;/select&amp;gt; &amp;lt;table&amp;gt; &amp;lt;thead&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Stars&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Language&amp;lt;/th&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/thead&amp;gt; &amp;lt;tbody&amp;gt; &amp;lt;tr ng-repeat=&amp;quot;repo in repos | filter:searchTerm | orderBy:reposSort&amp;quot;&amp;gt; &amp;lt;td&amp;gt;{{repo.name | uppercase}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.stargazers_count | number}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.language}}&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/tbody&amp;gt; &amp;lt;/table&amp;gt; &amp;lt;/div&amp;gt;
  • #13 **View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User: &amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt; &amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;div ng-show=&amp;quot;user&amp;quot;&amp;gt; &amp;lt;h1&amp;gt;&amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt; (created at: {{user.created_at | date:&amp;apos;short&amp;apos;}})&amp;lt;/h1&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;br&amp;gt; &amp;lt;h3&amp;gt;Repos&amp;lt;/h3&amp;gt; &amp;lt;input type=&amp;quot;search&amp;quot; placeholder=&amp;quot;search by repo name&amp;quot; ng-model=&amp;quot;searchTerm&amp;quot; /&amp;gt; Order: &amp;lt;select ng-model=&amp;quot;reposSort&amp;quot;&amp;gt; &amp;lt;option value=&amp;quot;+name&amp;quot;&amp;gt;Name&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;-stargazers_count&amp;quot;&amp;gt;Stars&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;+language&amp;quot;&amp;gt;Language&amp;lt;/option&amp;gt; &amp;lt;/select&amp;gt; &amp;lt;table ng-show=&amp;quot;repos&amp;quot;&amp;gt; &amp;lt;thead&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Stars&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Language&amp;lt;/th&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/thead&amp;gt; &amp;lt;tbody&amp;gt; &amp;lt;tr ng-repeat=&amp;quot;repo in repos | filter:searchTerm | orderBy:reposSort&amp;quot;&amp;gt; &amp;lt;td&amp;gt;{{repo.name | uppercase}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.stargazers_count | number}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.language}}&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/tbody&amp;gt; &amp;lt;/table&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; -------------------------------------------------------------- **Main View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User: &amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt; &amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;div ng-include=&amp;quot;&amp;apos;userdetails.html&amp;apos;&amp;quot; ng-show=&amp;quot;user&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; **Partial View (to be included)** &amp;lt;h1&amp;gt;&amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt; (created at: {{user.created_at | date:&amp;apos;short&amp;apos;}})&amp;lt;/h1&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;br&amp;gt; &amp;lt;h3&amp;gt;Repos&amp;lt;/h3&amp;gt; &amp;lt;input type=&amp;quot;search&amp;quot; placeholder=&amp;quot;search by repo name&amp;quot; ng-model=&amp;quot;searchTerm&amp;quot; /&amp;gt; Order: &amp;lt;select ng-model=&amp;quot;reposSort&amp;quot;&amp;gt; &amp;lt;option value=&amp;quot;+name&amp;quot;&amp;gt;Name&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;-stargazers_count&amp;quot;&amp;gt;Stars&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;+language&amp;quot;&amp;gt;Language&amp;lt;/option&amp;gt; &amp;lt;/select&amp;gt; &amp;lt;table ng-show=&amp;quot;repos&amp;quot;&amp;gt; &amp;lt;thead&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Stars&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Language&amp;lt;/th&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/thead&amp;gt; &amp;lt;tbody&amp;gt; &amp;lt;tr ng-repeat=&amp;quot;repo in repos | filter:searchTerm | orderBy:reposSort&amp;quot;&amp;gt; &amp;lt;td&amp;gt;{{repo.name | uppercase}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.stargazers_count | number}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.language}}&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/tbody&amp;gt; &amp;lt;/table&amp;gt;
  • #15 **Controller** var MainController = function ($scope, $http, $interval, $log) { var onUserComplete = function (response) { $scope.user = response.data; $http.get($scope.user.repos_url) .then(onRepos, onError); }, onError = function (reason) { $scope.error = &amp;quot;Could not fetch the data!&amp;quot;; }, onRepos = function (response) { $scope.repos = response.data; }, decrementCountdown = function () { $scope.countdown--; if ($scope.countdown &amp;lt; 1) { $scope.search($scope.username); } }, countdownInterval = null, startCountdown = function () { countdownInterval = $interval(decrementCountdown, 1000, $scope.countdown); }; $scope.search = function (username) { $log.info(&amp;apos;Searching for &amp;apos;+username); $http.get(&amp;quot;https://api.github.com/users/&amp;quot; + username) .then(onUserComplete, onError); if(countdownInterval) { $interval.cancel(countdownInterval); $scope.countdown = null; } } $scope.username = &amp;apos;angular&amp;apos;; $scope.reposSort = &amp;quot;-stargazers_count&amp;quot;; $scope.countdown = 5; startCountdown(); } **Main View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;br&amp;gt; {{countdown}} &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User: &amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt; &amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;div ng-include=&amp;quot;&amp;apos;userdetails.html&amp;apos;&amp;quot; ng-show=&amp;quot;user&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;
  • #16 **Controller** var MainController = function ($scope, $http, $interval, $log, $anchorScroll, $location) { var onUserComplete = function (response) { $scope.user = response.data; $http.get($scope.user.repos_url) .then(onRepos, onError); }, onError = function (reason) { $scope.error = &amp;quot;Could not fetch the data!&amp;quot;; }, onRepos = function (response) { $scope.repos = response.data; $location.hash(&amp;apos;userDetails&amp;apos;); $anchorScroll(); }, decrementCountdown = function () { $scope.countdown--; if ($scope.countdown &amp;lt; 1) { $scope.search($scope.username); } }, countdownInterval = null, startCountdown = function () { countdownInterval = $interval(decrementCountdown, 1000, $scope.countdown); }; $scope.search = function (username) { $log.info(&amp;apos;Searching for &amp;apos;+username); $http.get(&amp;quot;https://api.github.com/users/&amp;quot; + username) .then(onUserComplete, onError); if(countdownInterval) { $interval.cancel(countdownInterval); $scope.countdown = null; } } $scope.username = &amp;apos;angular&amp;apos;; $scope.reposSort = &amp;quot;-stargazers_count&amp;quot;; $scope.countdown = 5; startCountdown(); } **Partial View** &amp;lt;div id=&amp;quot;userDetails&amp;quot;&amp;gt; &amp;lt;h1&amp;gt;&amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt; (created at: {{user.created_at | date:&amp;apos;short&amp;apos;}})&amp;lt;/h1&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;br&amp;gt; &amp;lt;h3&amp;gt;Repos&amp;lt;/h3&amp;gt; &amp;lt;input type=&amp;quot;search&amp;quot; placeholder=&amp;quot;search by repo name&amp;quot; ng-model=&amp;quot;searchTerm&amp;quot; /&amp;gt; Order: &amp;lt;select ng-model=&amp;quot;reposSort&amp;quot;&amp;gt; &amp;lt;option value=&amp;quot;+name&amp;quot;&amp;gt;Name&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;-stargazers_count&amp;quot;&amp;gt;Stars&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;+language&amp;quot;&amp;gt;Language&amp;lt;/option&amp;gt; &amp;lt;/select&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;table ng-show=&amp;quot;repos&amp;quot;&amp;gt; &amp;lt;thead&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Stars&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Language&amp;lt;/th&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/thead&amp;gt; &amp;lt;tbody&amp;gt; &amp;lt;tr ng-repeat=&amp;quot;repo in repos | filter:searchTerm | orderBy:reposSort&amp;quot;&amp;gt; &amp;lt;td&amp;gt;{{repo.name | uppercase}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.stargazers_count | number}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.language}}&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/tbody&amp;gt; &amp;lt;/table&amp;gt; ----------Creating Services-------------------------------- **Conroller** var MainController = function ($scope, github, $interval, $log, $anchorScroll, $location) { var onUserComplete = function (data) { $scope.user = data; github.getRepos($scope.user).then(onRepos, onError); }, onError = function (reason) { $scope.error = &amp;quot;Could not fetch the data!&amp;quot;; }, onRepos = function (data) { $scope.repos = data; $location.hash(&amp;apos;userDetails&amp;apos;); $anchorScroll(); }, decrementCountdown = function () { $scope.countdown--; if ($scope.countdown &amp;lt; 1) { $scope.search($scope.username); } }, countdownInterval = null, startCountdown = function () { countdownInterval = $interval(decrementCountdown, 1000, $scope.countdown); }; $scope.search = function (username) { $log.info(&amp;apos;Searching for &amp;apos;+username); github.getUser(username).then(onUserComplete, onError); if(countdownInterval) { $interval.cancel(countdownInterval); $scope.countdown = null; } } $scope.username = &amp;apos;angular&amp;apos;; $scope.reposSort = &amp;quot;-stargazers_count&amp;quot;; $scope.countdown = 5; startCountdown(); } **Main View** &amp;lt;div ng-controller=&amp;quot;MainController&amp;quot;&amp;gt; &amp;lt;p&amp;gt; {{error}} &amp;lt;/p&amp;gt; &amp;lt;br&amp;gt; {{countdown}} &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User: &amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt; &amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;div ng-include=&amp;quot;&amp;apos;userdetails.html&amp;apos;&amp;quot; ng-show=&amp;quot;user&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; **Services** (function () { var github = function ($http) { var getUser = function (username) { return $http.get(&amp;quot;https://api.github.com/users/&amp;quot; + username) .then(function (response) { return response.data; }); }, getRepos = function (user) { return $http.get(user.repos_url) .then(function (response) { return response.data; }); }; return { getUser: getUser, getRepos: getRepos }; } var module = angular.module(&amp;apos;githubAPI&amp;apos;); module.factory(&amp;apos;github&amp;apos;, github); }());
  • #18 **The final code** **Module and routeProvider Config** (function () { var githubAPI = angular.module(&amp;apos;githubAPI&amp;apos;, [&amp;apos;ngRoute&amp;apos;]); githubViewer.config(function ($routeProvider) { $routeProvider .when(&amp;apos;/main&amp;apos;, { templateUrl: &amp;quot;main.html&amp;quot;, controller: &amp;apos;MainController&amp;apos; }) .when(&amp;apos;/user/:username&amp;apos;, { templateUrl: &amp;quot;user.html&amp;quot;, controller: &amp;apos;UserController&amp;apos; }) .otherwise({ redirectTo: &amp;apos;/main&amp;apos; }) }); }()); **First Controller** var MainController = function ($scope, $interval, $location) { var decrementCountdown = function () { $scope.countdown--; if ($scope.countdown &amp;lt; 1) { $scope.search($scope.username); } }, countdownInterval = null, startCountdown = function () { countdownInterval = $interval(decrementCountdown, 1000, $scope.countdown); }; $scope.search = function (username) { if(countdownInterval) { $interval.cancel(countdownInterval); $scope.countdown = null; } $location.path(&amp;apos;/user/&amp;apos;+username); } $scope.username = &amp;apos;angular&amp;apos;; $scope.countdown = 5; startCountdown(); } **Second Controller** var UserController = function ($scope, github, $routeParams) { var onUserComplete = function (data) { $scope.user = data; github.getRepos($scope.user).then(onRepos, onError); }, onError = function (reason) { $scope.error = &amp;quot;Could not fetch the data!&amp;quot;; }, onRepos = function (data) { $scope.repos = data; }; $scope.username = $routeParams.username; $scope.reposSort = &amp;quot;-stargazers_count&amp;quot;; github.getUser($scope.username).then(onUserComplete, onError); } **Main View** &amp;lt;div ng-view&amp;gt;&amp;lt;/div&amp;gt; **First Partial View** &amp;lt;div&amp;gt; &amp;lt;br&amp;gt; {{countdown}} &amp;lt;form name=&amp;quot;searchUser&amp;quot; ng-submit=&amp;quot;search(username)&amp;quot;&amp;gt; Search User: &amp;lt;input type=&amp;quot;search&amp;quot; ng-model=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username to find!&amp;quot; required /&amp;gt; &amp;lt;span&amp;gt;{{username}}&amp;lt;/span&amp;gt; &amp;lt;br&amp;gt; &amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;search&amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/div&amp;gt; **Second Partial View** &amp;lt;div&amp;gt; {{error}} &amp;lt;h1&amp;gt;&amp;lt;a ng-href=&amp;quot;{{user.html_url}}&amp;quot;&amp;gt;{{user.name}}&amp;lt;/a&amp;gt; (created at: {{user.created_at | date:&amp;apos;short&amp;apos;}})&amp;lt;/h1&amp;gt; &amp;lt;img ng-src=&amp;quot;{{user.avatar_url}}&amp;quot; title=&amp;quot;{{user.name}}&amp;quot; /&amp;gt; &amp;lt;br&amp;gt; &amp;lt;h3&amp;gt;Repos&amp;lt;/h3&amp;gt; &amp;lt;input type=&amp;quot;search&amp;quot; placeholder=&amp;quot;search by repo name&amp;quot; ng-model=&amp;quot;searchTerm&amp;quot; /&amp;gt; Order: &amp;lt;select ng-model=&amp;quot;reposSort&amp;quot;&amp;gt; &amp;lt;option value=&amp;quot;+name&amp;quot;&amp;gt;Name&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;-stargazers_count&amp;quot;&amp;gt;Stars&amp;lt;/option&amp;gt; &amp;lt;option value=&amp;quot;+language&amp;quot;&amp;gt;Language&amp;lt;/option&amp;gt; &amp;lt;/select&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;table ng-show=&amp;quot;repos&amp;quot;&amp;gt; &amp;lt;thead&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Stars&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Language&amp;lt;/th&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/thead&amp;gt; &amp;lt;tbody&amp;gt; &amp;lt;tr ng-repeat=&amp;quot;repo in repos | filter:searchTerm | orderBy:reposSort&amp;quot;&amp;gt; &amp;lt;td&amp;gt;{{repo.name | uppercase}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.stargazers_count | number}}&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;{{repo.language}}&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;/tbody&amp;gt; &amp;lt;/table&amp;gt; &amp;lt;br&amp;gt; &amp;lt;a href=&amp;quot;#/main&amp;quot;&amp;gt;Back To Search!&amp;lt;/a&amp;gt; **Services** (function () { var github = function ($http) { var getUser = function (username) { return $http.get(&amp;quot;https://api.github.com/users/&amp;quot; + username) .then(function (response) { return response.data; }); }, getRepos = function (user) { return $http.get(user.repos_url) .then(function (response) { return response.data; }); }; return { getUser: getUser, getRepos: getRepos }; } var module = angular.module(&amp;apos;githubAPI&amp;apos;); module.factory(&amp;apos;github&amp;apos;, github); }());