Unable to inject a factory into an AngularJS controller

I just started learning angulerJS and created a factory to retrieve data from an API. However, I encountered an error when trying to use the factory in a controller.

Below is the code for the factory:

(function () {

    var CategoriesFactory = function($http) {
        var factory = {};

        factory.getCategorys = function(account_id){
            return $http.get('http://localhost:18678/api/Transaction?account_id=2');
        };

        factory.getTransaction = function(acc_id){
            return $http.get('http://localhost:18678/api/Transaction?acc_id=2');
        };

        factory.getTransactionInCategory = function(category_id, from_date, to_date){
            return.$http.get('http://localhost:18678/api/transaction?category='+category_id+'&account=2&from=2015-01-                              01&to=2015-12-30');
        };
        return factory;
    };

    angular.module('AccApp').factory('CategoriesFactory', CategoriesFactory);
 }());  

Next is the controller:

app.controller('CategoriesController',
  function ($scope, $routeParams, $http, CategoriesFactory) {

  })

And here is the error message: Unknown provider: CategoriesFactoryProvider <- CategoriesFactory

Answer №1

Seems like you may have overlooked injecting AccApp into your main module where your app is defined.

angular.module("app", ['AccApp']);

Ensure to include it in this manner.

Trust this solution proves helpful!

Answer №2

Why are you attempting to write Angular code in such a peculiar manner? The essence of Angular is simplicity.

 var app = angular.module('AccApp',[]);

    app.factory('CategoriesFactory',function($http){// feel free to move this factory into its own file
       return{
              getCategorys:function(account_id){
               return $http.get('http://localhost:18678/api/Transaction?account_id=2');
                },
              getTransaction:function(acc_id){
               return $http.get('http://localhost:18678/api/Transaction?acc_id=2');
                },
                getTransactionInCategory : function(category_id, from_date, to_date){
             return.$http.get('http://localhost:18678/api/transaction?category='+category_id+'&account=2&from=2015-01-                              01&to=2015-12-30');
                };
              }
      });

You can now easily inject this factory into your controller:

     app.controller('CategoriesController',function ($scope, $routeParams, $http, CategoriesFactory){

       console.log(CategoriesFactory.getCategorys(getCategorys));
       });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can AngularJS functions be saved in arrays and then assigned to ng-click?

I have been attempting to create buttons using an Array in my code. While the buttons are displayed on the screen, the functions linked to each button in the array are not working as expected. var appModule = angular.module('app',[]); appModul ...

What is the best method for determining the height of a sandboxed iframe?

My website includes an iframe with the sandbox attribute. Users can set the content of this iframe directly through the 'srcdoc' attribute, which raises security concerns. I am trying to find a way to dynamically adjust the height of the iframe ...

Deleting a user in Vue.js using Firebase UID

I'm working on implementing a function in my Vue.js Firebase application that allows users to be deleted by UID. The app is designed to allow user registration through email and password authentication in Firebase. Once a user is logged in, they shoul ...

Having trouble invoking PHP functions from AngularJS

Hey there, I'm a complete beginner in web development. I have an HTML file with AngularJS code that calls a .php file, both located in my local folder. I've tried looking online, but: Installing a server is not possible as this will be used ...

Unit testing React Native for inputting text values

I am attempting to append a unit to the value of a textInput: https://i.sstatic.net/EqswS.jpg I have attempted to achieve this with the following code but have not been successful: value = { this.state.totalWeight + " Kgs"} I also tried adding ...

Having trouble getting two components to return in React

After successfully implementing the Hello World example by returning "Hello" and "world" from two different components, I encountered a problem with my code. In this specific code snippet, I am unable to return the Menubar component, although the Map compo ...

Utilizing Python for extracting text from dynamically generated web pages with JavaScript

Is there a way to write a script in Linux that can extract text from a page generated by Javascript, specifically using etherpad (for example, http://www.board.net)? I have not been able to find an existing tool that fits my needs, such as lynx which doesn ...

Retrieve the output from PHP in JSON format and then utilize jQuery to parse it

I am currently working on a jQuery function $.post(file.php), where the data is being sent to "file.php" and returned in JSON format using json_encode(). Although I am able to successfully retrieve the result, I am unsure how to separate the individual i ...

Using Javascript, Typescript, and Angular 5 to access and view a file

Currently, I am utilizing Angular 5 for a project that involves the need to extract an AMP HTML file as plain text. The specific file is stored within a component and must be accessed solely from that component. My primary goal is to have the capability t ...

ThymeLeaf fails to display the elements of a List

I am struggling with a complex HTML structure in my project. The HTML code includes a mix of Bootstrap classes and Thymeleaf attributes. Additionally, there is a JavaScript function that interacts with radio buttons to show or hide elements based on user i ...

Submitting information to the server utilizing a POST request through jQuery

What I'm attempting to achieve: I am currently working on sending data to my server using the $.post jQuery shortcut. The issue at hand: It seems that my program is not entering my switch case, possibly due to a problem with sending the action prop ...

Error TS2339 encountered in ngx-uploader: There is no property called 'lastModifiedDate' on the type 'File'

An issue has arisen in the code at node_modules/ngx-uploader/src/ngx-uploader/classes/ngx-uploader.class.ts(112,32): error TS2339: Property 'lastModifiedDate' is not found on type 'File'. Encountered this error while updating my An ...

A guide to toggling SVG elements with AngularJS

Is it possible to toggle between two different svg images using the ng-include directive? For example, I currently have the following code snippet: <i class="indicator oppcicon {{domain.show_requests ? 'icon-chevron-down' : 'icon-chevro ...

Adding HTML and scripts to a page using PHP and JS

Currently, I am utilizing an ajax call to append a MVC partial view containing style sheets and script files to my php page. Unfortunately, it seems that the <script> tags are not being appended. After checking my HTTP request on the network, I can ...

Looking to extract data from various checkbox options and save it as an array variable

For a coding boot camp assignment, I'm working on a modal that includes options for the days of the week. My approach involves using Jquery .each and CSS :checked to retrieve the values assigned to each input. However, every time I attempt to log the ...

Firebase Functions utilizing Realtime Database to update nested child data

Every 10 minutes, a child is created inside my Realtime Database in Firebase through a webservice that sends data via the Rest API. When this data arrives, a function picks it up. The node contains various values, but the important one for further processi ...

Sharing data with external domains and retrieving HTML output using JavaScript

When a browser sends a POST request and expects an HTML page result from JavaScript, problems arise if there is no Access-Control-Allow-Origin in the server response. Unfortunately, changing anything on the server side is not an option. If a human clicks ...

Discover the process of selecting an item from a list and viewing it on a separate page with the help of AngularJS and Ionic technology

I am struggling with creating a list of items that, when clicked, should take me to the corresponding post. Despite trying to use ng-click in the header, I suspect there is an issue with the route not functioning properly. As part of my testing process, I ...

Utilizing Window function for local variable assignment

Currently, I am facing a challenge in my Angular2 service where I am attempting to integrate the LinkedIN javascript SDK provided by script linkedin. The functionality is working as expected for retrieving data from LinkedIN, however, I am encountering an ...

Tips for calculating the total of a virtual column through child associations in Sequelize

I have a network of 3 interconnected objects (A, B, and C). There can be multiple Bs associated with A, and multiple Cs associated with each B. For instance: A └───B │ └───C │ └───C └───B └───C Object ...