AngularJS Lesson 3 : Some Points on ng-controller

These are some points
  • This directive is used to bind the scope of the view with the controller.
  • If your controller is performing too much processing, you can use nested controllers .i.e. a controller within another to delegate specific behavior to another controller.
    1. 8) <body ng-controller="ParentCtrl">
    2. 9) <input ng-model="name" /> {{name}}
    3. 10)
    4. 11) <div ng-controller="ChildCtrl">
    5. 12) <input ng-model="name" /> {{name}} - {{$parent.name}}
    6. 13) </div>
    7. 14) </body>
  • Controllers are useful in sharing objects or create common behavior for event callback functions.
  • Controllers are classes which indicate Angular about the application model by connecting them to $scope object passed into your controller.
  • The controller uses parameters which are dependencies, injected by AngularJS injector service. These are services that are used inside the controller.
  • The DI in Angular works by matching the names of the parameters, when we declare function as function someController($scope) {}, the injector finds the  $scope service from the parameter name.
  • The following is an example usage of ng-controller, it has 2 files the main html file and the controller file;
  • ControllerTest.html
    1. <html>
    2. <head>
    3. <title>Angular JS Modules</title>
    4. <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    5. <script src = "controller1.js"></script>
    6. <meta charset="utf-8">
    7. <meta name="viewport" content="width=device-width, initial-scale=1">
    8. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    9. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    10. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    11. </head>
    12. <body>
    13. <div class="jumbotron">
    14. <h1>ng-controller</h1>
    15. </div>
    16. <div ng-app = "appMainModule" ng-controller = "teamsController">
    17. <table class="table table-striped" border=1>
    18. <tr>
    19. <th>Team Name </th>
    20. <th>City </th>
    21. <th>Rank </th>
    22. </tr>
    23. <tr ng-repeat = "team in team.info">
    24. <td>{{ team.name }}</td>
    25. <td>{{ team.city }}</td>
    26. <td>{{ team.rank }}</td>
    27. </tr>
    28. </table>
    29. </div>
    30. </body>
    31. </html>
Controller.js
  1. var appMainModule = angular.module("appMainModule", []);
  2. appMainModule.controller("teamsController", function($scope) {
  3. $scope.team = {
  4. info:[
  5. {name:'Manchester City',city:'Manchester',rank:1},
  6. {name:'Arsenal',city:'London',rank:2},
  7. {name:'Manchester United',city:'Manchester',rank:3},
  8. {name:'Leicester City',city:'Leicester',rank:4},
  9. {name:'Liverpool',city:'Liverpool',rank:5}
  10. ],
  11. currentState: function() {
  12. var teamObject;
  13. teamObject = $scope.team;
  14. return teamObject.name + " " + teamObject.city+" "+teamObject.rank;
  15. }
  16. };
  17. });
The following is the output we get:
AngularJS ng-controller
                                                                                                                           AngularJS ng-controller

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes