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>

1 Answer 1

1
"use strict"; 

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

     $scope.trucks = trucksService.getTrucks();

}]);
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Mihail, I tried that, its still not working. Thank you.
What is the error in the developer tools in your browser?
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.
Error: [ng:areq] Argument 'ext-modules/Fleet/jsFleetController.js' is not a function, got undefined errors.angularjs.org/1.4.8/ng/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.