0

I am trying to make a REST request with angularjs.

Main.js

angular.module('frontendApp')
  .controller('MainCtrl', ['$scope', '$http', function($scope, $http) 
  {

  $scope.loadData = function() {
     $scope.services = Services.query();

};

services.js

'use strict';

angular.module('frontendApp').factory('Services', function($resource) {
  return $resource('/services/:serviceId', {
    serviceId: '@_id'
  }, {});
});

I get an error: Services is not defined. What did I wrong ?

EDIT: Mains.html

<div class="jumbotron">


Choose A Category1
 <div class="wrap" ng-controller="MainCtrl">
<select ng-model="selectedValue" ng-change="loadData()" >
  <option value="1">Category 1</option>
  <option value="2">Category 2</option>
  <option value="3">Category 3</option>
  <option value="4">Category 4</option>
  <option value="5">Category 5</option>
</select>



<div ng-show="selectedValue != null" class="main">
  <center><h2>Results For Category  {{ selectedValue }}</h2></center> <br><br>

</div>


<div  class="main"  >
  <ul class="cloudlist">
    <li class="service" ng-repeat="item in services" ng-click="select(item)">
      <div class="info">

        <h3>{{item.service_name}}</h3>
        <b>{{item.status_page}}</b><br>
        <b>Is Billed : {{item.is_billed.billing_term._identifier}}</b>


      </div>
    </li>
  </ul>
  <br>
</div>

</div>

After injecting the Service in the controller

angular.module('frontendApp')
    .controller('MainCtrl', ['$scope', '$http', 'Services', function ($scope, $http, Services) {
        $scope.loadData = function () {
            $scope.services = Services.query();
        };

I get this error: Unknown provider: ServicesProvider <- Services <- MainCtrl

3
  • 1
    did yo injected ngResouce module and its angular-ng-resource.js like angular.module('frontendApp',['ngResource']) Commented Jun 13, 2015 at 18:01
  • Have you loaded your services.js file before loading main.js Commented Jun 13, 2015 at 18:37
  • I don't understand what do you exactly mean ... Commented Jun 13, 2015 at 18:44

1 Answer 1

1

You have to inject Services in your controller.

var module = angular.module('frontendApp', ['ngResource']);

module.factory('MyServices', ['$resource', function($resource) {
    return $resource('/services/:serviceId', {
        serviceId: '@_id'
    }, {});
}]);

module.controller('MainCtrl', ['$scope', '$http','MyServices', function ($scope, $http, MyServices) {
    $scope.loadData = function () {
        $scope.services = MyServices.query();
    };
}]);
Sign up to request clarification or add additional context in comments.

5 Comments

I have this error when I do it: Unknown provider: ServicesProvider <- Services <- MainCtrl
Error: Services is undefined $scope.loadData@localhost:9000/scripts/controllers/main.js:13:13
you include the services.js before main.js in your HTML file?
I call services in main.js, I am not including anything, I added my html
try to change factory with service

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.