0

I have a service file which needs to be called from the controller. Can someone please tell the code which should go in the controller to get this service file. Thank you.


This is my service file code

"use strict";

angular.module('jsFleet').service('trucksService',

       function () {
           this.getTrucks = function () {
               return trucks;
           };
           this.getTruck = function (truckID) {
               for (var i = 0, len = trucks.length; i < len; i++) {
                   if (trucks[i].truckID === parseInt(truckID)) {
                       return trucks[i];
                   }
               }
               return {};
           };
           var trucks = [
               {
                   truckID: 1,
                   status: Running,
                   destination: WPG,
                   alerts: Nothing
               },
                   {
                       truckID: 5,
                       status: Running,
                       destination: WPG,
                       alerts: Nothing
                   },
                   {
                       truckID: 2,
                       status: Running,
                       destination: WPG,
                       alerts: Nothing
                   },
                   {
                       truckID: 3,
                       status: Running,
                       destination: WPG,
                       alerts: Nothing
                   },
                   {
                       truckID: 4,
                       status: Running,
                       destination: WPG,
                       alerts: Nothing
                   }
           ];

       });

This is my controller code

"use strict"; 

angular.module("jsFleet").controller("jsFleetController",
    ['$scope', 'trucksService', function ($scope, trucksService) {




    }]);

This is my HTML code

<div class="panel panel-primary">
    <div class="panel-heading" align="center">TRUCKS</div>
        <table class="table table-bordered table-condensed table-striped">
             <tbody>
                <tr>
                    <th>TruckID</th>
                    <th>Status</th>
                    <th>Dest.</th>
                    <th>Alerts</th>
                </tr>
            <tr ng-repeat="row in trucks">
                <td>{{row.truckID}}</td>
                <td>{{row.status}}</td>
                <td>{{row.destination}}</td>
                <td>{{row.alerts}}</td>
            </tr>      
            </tbody>
        </table>
    </div>
Gaurav Ram
  • 1,085
  • 3
  • 16
  • 32

1 Answers1

1
"use strict"; 

angular.module("jsFleet").controller("jsFleetController", 
  ['$scope', 'trucksService', function ($scope, trucksService) {

     $scope.trucks = trucksService.getTrucks();

}]);
Mihail Petkov
  • 1,535
  • 11
  • 11
  • Hi Mihail, I tried that, its still not working. Thank you. – Gaurav Ram Jan 04 '16 at 22:49
  • What is the error in the developer tools in your browser? – Mihail Petkov Jan 04 '16 at 22:50
  • Error: [ng:areq] http://errors.angularjs.org/1.4.8/ng/areq?p0=ext-modules%2FFleet%2FjsFleetController.js&p1=not%20aNaNunction%2C%20got%20undefined at Error (native) at http://localhost:51340/scripts/angular.min.js:6:416 at qb (http://localhost:51340/scripts/angular.min.js:22:131) at Qa (http://localhost:51340/scripts/angular.min.js:22:218) ..... at g (http://localhost:51340/scripts/angular.min.js:55:105) at http://localhost:51340/scripts/angular.min.js:54:249(anonymous function) @ angular.min.js:107 – Gaurav Ram Jan 04 '16 at 22:59
  • Change your angular version from `localhost:51340/scripts/angular.min.js` to `localhost:51340/scripts/angular.js` because angular is minified and I can't understand the error. – Mihail Petkov Jan 04 '16 at 23:00
  • Error: [ng:areq] Argument 'ext-modules/Fleet/jsFleetController.js' is not a function, got undefined http://errors.angularjs.org/1.4.8/ng/areq?p0=ext-modules%2FFleet%2FjsFleetController.js&p1=not%20aNaNunction%2C%20got%20undefined – Gaurav Ram Jan 04 '16 at 23:05
  • The error is too lengthy to insert here. I'll break and send you. First half: 'Error: [ng:areq] Argument 'ext-modules/Fleet/jsFleetController.js' is not a function, got undefined http://errors.angularjs.org/1.4.8/ng/areq?p0=ext-modules%2FFleet%2FjsFleetController.js&p1=not%20aNaNunction%2C%20got%20undefined at http://localhost:51340/scripts/angular.js:68:12 at assertArg (http://localhost:51340/scripts/angular.js:1815:11) at assertArgFn (http://localhost:51340/scripts/angular.js:1825:3) at http://localhost:51340/scripts/angular.js:9158:9' – Gaurav Ram Jan 04 '16 at 23:14
  • Second half: ' at A.link (http://localhost:51340/scripts/angular-route.min.js:7:268) at invokeLinkFn (http://localhost:51340/scripts/angular.js:8841:9) at nodeLinkFn (http://localhost:51340/scripts/angular.js:8335:11) at compositeLinkFn (http://localhost:51340/scripts/angular.js:7731:13) at publicLinkFn (http://localhost:51340/scripts/angular.js:7611:30) at boundTranscludeFn (http://localhost:51340/scripts/angular.js:7749:16) – Gaurav Ram Jan 04 '16 at 23:16
  • Did you add `jsFleet`module in your app.js file? – Mihail Petkov Jan 04 '16 at 23:27
  • `angular.module("app", ["ngRoute","jsFleet"]);` Yes i did. It is giving same error still. – Gaurav Ram Jan 04 '16 at 23:34
  • Hi Mihail, can you please check this question and tell where the mistake is? Thank you. http://stackoverflow.com/q/34668416/5724169 – Gaurav Ram Jan 09 '16 at 20:14