Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:32:20+00:00 2026-06-16T17:32:20+00:00

I’m trying to convert an Angular HTTP.get function in my controllers.js to a service

  • 0

I’m trying to convert an Angular HTTP.get function in my controllers.js to a service in services.js.

The examples I have found all have conflicting ways to implement the service and their choice of names is confusing. Furthermore, the actual angular documentation for services uses yet a different syntax than all the examples. I know this is super simple, but please help me out here.

I have app.js, controllers.js, services.js, filters.js.

app.js

angular.module('MyApp', []).
    config(['$routeProvider', function($routeProvider)
        {
            $routeProvider.
                when('/bookslist', {templateUrl: 'partials/bookslist.html', controller:             BooksListCtrl}).
                otherwise({redirectTo: '/bookslist'});
        }
    ]);

controllers.js

function BooksListCtrl($scope,$http) {
    $http.get('books.php?action=query').success(function(data) {
        $scope.books = data;
    });

    $scope.orderProp = 'author';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:32:22+00:00Added an answer on June 16, 2026 at 5:32 pm

    Think in terms of modules and dependency injection.

    So, lets say you have these three files

    <script src="controllers.js"></script>
    <script src="services.js"></script>
    <script src="app.js"></script>
    

    You would need three modules

    1. Main App Module

    angular.module('MyApp', ['controllers', 'services'])
      .config(['$routeProvider', function($routeProvider){
        $routeProvider
          .when('/bookslist', {
            templateUrl: 'partials/bookslist.html', 
            controller:  "BooksListCtrl"
          })
          .otherwise({redirectTo: '/bookslist'});
      }]);
    

    Notice that the other two modules are injected into the main module, so their components are available to the whole app.

    2. Controllers Module

    Currently your controller is a global function, you might want to add it into a controllers module

    angular.module('controllers',[])
      .controller('BooksListCtrl', ['$scope', 'Books', function($scope, Books){
        Books.get(function(data){
          $scope.books = data;
        });
    
        $scope.orderProp = 'author';
      }]);
    

    Books is passed to the controller function and is made available by the services module which was injected in the main app module.

    3. Services Module

    This is where you define your Books service.

    angular.module('services', [])
      .factory('Books', ['$http', function($http){
        return{
          get: function(callback){
              $http.get('books.json').success(function(data) {
              // prepare data here
              callback(data);
            });
          }
        };
      }]);
    

    There are multiple ways of registering services.

    • service: is passed a constructor function (we can call it a class) and returns an instance of the class.
    • provider: the most flexible and configurable since it gives you access to functions the injector calls when instantiating the service
    • factory: is passed a function the injector invokes when instantiating the service.

    My preference in using the factory function and just have it return an object. In the example above we just return an object with a get function that is passed a success callback. You could change it to pass an error function too.


    Edit Answering @yair’s request in the comments, here’s how you would inject a service into a directive.

    4. Directives Module

    I follow the usual pattern, first add the js file

    <script src="directives.js"></script>
    

    Then define a new module and register stuff, in this case a directive

    angular.module('directives',[])
      .directive('dir', ['Books', function(Books){
        return{
          restrict: 'E',
          template: "dir directive, service: {{name}}",
          link:function(scope, element, attrs){
            scope.name = Books.name;
          }
        };
      }]);
    

    Inject the directive module to the main module in app.js.

    angular.module('MyApp', ['controllers', 'services', 'directives']) 
    

    You might want to follow a different strategy and inject a module into the directives module

    Notice that the syntax inline dependency annotation is the same for almost everything. The directive is injected the same Books service.

    Updated Plunker: http://plnkr.co/edit/mveUM6YJNKIQTbIpJHco?p=preview

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.