Troubleshooting Problem: Difficulty accessing Controller in AngularJS Module

I am facing difficulties with communication between my application and a module that I have developed. Below is the AngularJS module that I created.

(function (document, window) {

    'use strict';

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

    piCart.config(['$routeProvider', function($routeProvider){
        $routeProvider.
            when('/cart', {
                templateUrl: "packages/pi-cart/segments/cart.html",
                controller: 'CartController',
                private : true
            }).
            when('/checkout', {
                template: "Checkout Page",
               // controller: 'CartController',
                private : true
            });
    }]);

    piCart.factory('TestFactory', function(){
        return{
            test : function(){
                return 'test works';
            }
        }
    });

    piCart.controller("CartController",function(TestFactory){
        console.log("Cart Controller Running");
        console.log(TestFactory.test());
    });

})(document, window);

This module is loaded into my main application like this:

var app = angular.module('app', ['ngRoute', "ui.bootstrap", "googlechart", "piCart"]);

I am attempting to access the TestFactory module from the app.controller in the following way:

app.controller('ProductController',function($scope){

  $scope.addToCart = function(id){
    //alert("clicked: "+id);
    test = TestFactory.test();
    console.log(test);
  };

});

However, I encounter the error message:

ReferenceError: TestFactory is not defined

Answer №1

It appears that the error is occurring due to the absence of TestFactory injection in the controller:

app.controller('ProductController', function($scope, TestFactory) {
    // your code...
});

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

Issues encountered with jQuery's $.ajax function when communicating with PHP

I am having trouble creating a simple app that displays data from a MySQL database using PHP and jQuery. The issue I am facing is with retrieving the data using jQuery. While my PHP script successfully returns the data without any problems, I am not receiv ...

Encountering a failure when trying to run npm in a React project

I've removed the .lock file and node_modules, then reinstalled, but it's still not working. Can someone assist me with fixing this? npm ERR! gyp ERR! node -v v16.13.0 npm ERR! gyp ERR! node-gyp -v v8.2.0 npm ERR! gyp ERR! not ok npm ERR! node-pr ...

creating a list of checkboxes with v-for in Vue.js

Having a bit of trouble with CheckBox selection. I am looping through a DataTable and adding CheckBox to it, storing them as an Array. My goal is to have the functionality where selecting the left checkbox removes the right one, and vice versa for the ri ...

Adjusting the view to focus solely on the visible portion of the webpage

Is there a way to achieve zooming in and out on a website similar to how it works on the site ? I want only the visible area to zoom in or out when users interact with their browser. I searched online for a solution but couldn't find one. Any suggesti ...

Learn how to trigger the keydown function in VUE programming

I am trying to create a method that will be triggered whenever any key on the keyboard is pressed, regardless of where the focus is. I want to be able to determine which key was pressed in this method. Currently, I have set up an event listener for keydow ...

Vue does not consistently update HTML when the reference value changes

I am trying to showcase the readyState of a WebSocket connection by utilizing a Ref<number> property within an object and displaying the Ref in a template. The value of the Ref is modified during WebSocket open and close events. However, I am encount ...

Emulate the CSS hover effect with jQuery 코드 희미한

Is there a method in jquery or any other technology that can detect event X and trigger a different event elsewhere? Currently, I am working with an image that has an image map. When a user hovers over a specific area on the map, I would like another part ...

HTML code featuring multiple dropdown menus, each equipped with its own toggleable textarea

I have multiple HTML drop downs, each triggering a textarea based on the selection. Currently, I'm using show and hide JavaScript functions for each question individually. Is there a way to streamline this so that I don't have to write separate c ...

Encountering a 404 error on package.json when utilizing SystemJS with Karma

Issue: I'm facing challenges while trying to incorporate SystemJS config with Karma using the karma-systemjs plugin. Karma is throwing errors indicating that it cannot locate the package.json file for each import: ... 10 07 2017 13:38:15.107:WARN [ ...

Create a regex pattern that can accurately extract email addresses from a comma-del

Currently, I have a regular expression that validates a single email address. How can I modify this regex to accept a list of email addresses separated by commas? ^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A- ...

Encountering an error while using TypeScript, Mocha, and Express: "TypeError: app.address is not a

While transitioning an API from ES6 to TypeScript, a particular issue arises when attempting to run unit tests on the Express REST Endpoints: TypeError: Cannot read property 'address' of undefined The server code has been slightly adjusted for ...

Interactive div containing elements that cannot be clicked

http://codepen.io/anon/pen/zxoYBN To demonstrate my issue, I created a small example where there is a link button within a div that toggles another div when clicked. However, I want the link button to not trigger the toggle event and be excluded from the ...

What is the best way to showcase a user's input in a webpage using vanilla javascript

Struggling with creating functionalities for a simple calculator using vanilla.js. Able to display numbers on click but facing issues with multiple clicks and deletion of values. Trying to use addeventlistener but encountering a Type Error "addeventliste ...

Error: Authorization token is required

For email confirmation, I am utilizing JWT. An email is sent to the user with a URL containing the token. Here's an example of the URL received by the user: http://localhost:3000/firstlogin?acces_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI ...

Bootstrap modal experiencing technical difficulties

I am experiencing issues with my bootstrap modal. It seems to be malfunctioning, almost as if the CSS or JavaScript files are not being recognized. I have tried various solutions but have been unable to resolve the problem. I even attempted using the examp ...

Angular is having trouble parsing Json data which is resulting in a blank screen

I'm struggling with parsing a JSON file. In order to parse an array, I'm using the following code snippet: {"records":[{"id":"1","first_name":"John","last_name":"Doe"},{"id":"2","first_name":"Jane","last_name":"Doe"},{"id":"3","first_name":"Jo ...

I'm having trouble with my AJAX Update Panel. Can someone please help me figure out what I'm doing wrong?

--------------Handling Gridviews DataBound Event ----------------- protected void grdShowCallingList_DataBound(object sender, EventArgs e) { if (grdShowCallingList.Rows.Count > 0) { foreach (GridViewRow row in grdShowCallingList.Rows) ...

When ng-click is utilized, AngularJS ui-router falls back to the default route

Currently experimenting with AngularJS as a learning project. Trying to familiarize myself with AngularJS ui-router (https://github.com/angular-ui/ui-router) and encountering a roadblock with an issue. Created a plunk to showcase the problem. http://plnk ...

If the request body exists, it should return a 409 error code

*Can anyone please help me with avoiding duplicate requests for existing names in NodeJS Express?* Here is my code: /* Post new person to persons */ app.post("/api/persons/", (req, res) => { const schema = { name: Joi.string().alphanum ...

"What is the best way to access and extract data from a nested json file on an

I've been struggling with this issue for weeks, scouring the Internet for a solution without success. How can I extract and display the year name and course name from my .json file? Do I need to link career.id and year.id to display career year cours ...