For creating controller and custom filters in Angular applications follow the below steps,
  1. Create a Student controller as StudentController.js. Add the following code to it to create a new module,
    1. var app = angular.module('app', []);
  2. Now create a controller in controller file and create AvgMarks function. Add the following code in Student controller,
    1. app.controller('studentController', function($scope)
    2. {
    3. $scope.helloAngular = 'I Learn';
    4. $scope.FirstName = 'Deepak';
    5. $scope.LastName = 'Upreti';
    6. $scope.studentdata = [
    7. {
    8. Name: 'Deepak Upreti',
    9. Id: 1,
    10. Sub1Marks: 80,
    11. Sub2Marks: 50,
    12. Sub3Marks: 40
    13. },
    14. {
    15. Name: 'Manu',
    16. Id: 2,
    17. Sub1Marks: 80,
    18. Sub2Marks: 70,
    19. Sub3Marks: 60
    20. }
    21. ];
    22. $scope.AvgMarks = function(sub1, sub2, sub3) {
    23. var total = sub1 + sub2 + sub3;
    24. return (total / 3);
    25. };
    26. });
  3. Now create a custom Filter as nameReverse for reversing the name. Also if you want you can create a separate JS file for the filter's logical separation. Add the following code,
    1. app.filter("nameReverse", function()
    2. {
    3. return function(name)
    4. {
    5. return name.split("").reverse().join("");
    6. };
    7. });
  4. Now insert the following code to your view, in this case, Index.cshtml file,
    1. <!DOCTYPE html>
    2. <html ng-app="app">
    3. <head>
    4. <title> Angular Page</title>
    5. </head>
    6. <body>
    7. <div ng-controller="studentController">
    8. {{helloAngular}}
    9. </div>
    10. <div ng-controller="studentController">
    11. Full Name: {{FirstName + " " + LastName}}
    12. </div>
    13. <br/><br/><br/>
    14. <div class="table-responsive">
    15. <table border="1" style="width:800px" ng-controller="studentController">
    16. <tr>
    17. <td><b>Name</b></td>
    18. <td><b>ReverseName</b></td>
    19. <td><b>Id</b></td>
    20. <td><b>Sub1Marks</b></td>
    21. <td><b>Sub2Marks</b></td>
    22. <td><b>Sub3Marks</b></td>
    23. <td><b>AverageMarks</b></td>
    24. </tr>
    25. <tr ng-repeat="s in studentdata">
    26. <td>{{s.Name}}</td>
    27. <td>{{s.Name | nameReverse}}</td>
    28. <td>{{s.Id}} </td>
    29. <td>{{s.Sub1Marks}} </td>
    30. <td>{{s.Sub2Marks}} </td>
    31. <td>{{s.Sub3Marks}} </td>
    32. <td>{{AvgMarks(s.Sub1Marks,s.Sub2Marks,s.Sub3Marks)}} </td>
    33. </tr>
    34. </table>
    35. </div>
    36. <script src="../../Scripts/angular.min.js"></script>
    37. <script src="../../Scripts/Controller/StudentController.js"></script>
    38. </body>
    39. </html>
  5. Run the application and you will get the following output,
  6. Now test the controller and new custom filter we created (“nameReverse”)
  • Create a folder as a Unit Test. And add a JavaScript file as appfilterTest. Add the following code to it,
    1. /// <reference path="../Scripts/angular.js" />
    2. /// <reference path="../Scripts/angular-mocks.js" />
    3. /// <reference path="../Scripts/Controller/StudentController.js" />
    4. describe('Reverse Filter', function()
    5. {
    6. var reverseFilter;
    7. beforeEach(module('app'));
    8. var scope;
    9. beforeEach(inject(function($controller, $rootScope) {
    10. scope = $rootScope.$new();
    11. var ctrl = $controller('studentController', {
    12. $scope: scope
    13. });
    14. }));
    15. it('AvgMarks function test', function() {
    16. var result = scope.AvgMarks(4, 4, 4);
    17. expect(result).toEqual(4);
    18. });
    19. beforeEach(inject(function(_nameReverseFilter_) {
    20. reverseFilter = _nameReverseFilter_;
    21. }));
    22. it('testing reverse filter', function() {
    23. expect(reverseFilter('deepak')).toEqual('kapeed');
    24. });
    25. });
  • Go ahead and run the test cases and you will get the result as testing reverse filter and AvgMarks function test as PASS,