Angular experiencing an issue with injector functionality

I encountered an error while using my app:

Error: [$injector:unpr] http://errors.angularjs.org/1.3.2/$injector/unpr?p0=ProductResourceProvider%20%3C-%20ProductResource

The situation is as follows: the app is designed to retrieve information from a web service, but I want to simulate loading products using ng mock if the web service is not available.

My code in the head tag looks like this:

<script src="//code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js" type="text/javascript"></script>
    <script src="//code.angularjs.org/1.3.2/angular-resource.min.js" type="text/javascript"></script>
    <script src="//code.angularjs.org/1.3.2/angular-mocks.js" type="text/javascript"></script>

    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.2.js"c></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>|

    <!-- Application Script -->
    <script src="../Scripts/App/App.js" type="text/javascript"></script>

    <!-- Services -->
    <script src="../Scripts/App/Common/Services/common.services.js"  type="text/javascript"></script>
    <script src="../Scripts/App/Common/Services/productResource.js"></script>
    <script src="../Scripts/App/Common/Services/productResourceMock.js" type="text/javascript"s></script>

    <!--Controllers -->
    <script src="../Scripts/App/Products/ProductListCtrl.js" type="text/javascript"></script>

The App.Js file defines the module and its dependencies:

(function () {
    "use strict";
    var app = angular.module("productManagement", ["common.services", "productResourceMock"]);
}());

The Product Controller is simple:

(function () {
    "use strict";
    angular
        .module("productManagement")
        .controller("ProductListCtrl",
                    ["ProductResource",
                    ProductListCtrl]);

    function ProductListCtrl(productResource) {
        var vm = this;

        productResource.query(function (data) {
            vm.products = data;
        });

        vm.showImage = false;

        vm.toggleImage = function () {
            vm.showImage = !vm.showImage;
        }
    }
}());

The Common.Services file includes ng-resource:

(function () {
    "use strict";

    angular
        .module("common.services",
                ["ngResource"])
}());

Next, we have the product resource:

(function () {
        "use strict";

        angular
            .module("common.services")
            .factory("productResource",
                    ["$resource",
                     productResource]);

        function productResource($resource) {
            return $resource("/api/products/:productId")
        }

    }());

And finally, the product resource mock:
    
(function () {
    "use strict";

    var app = angular
                .module("productResourceMock",
                        ["ngMockE2E"]);

    app.run(function ($httpBackend) {
        // Mock product data
    })
}());

I have double-checked for any spelling mistakes, but I am still unable to pinpoint the issue. Any ideas on what might be missing?

Lastly, here's the remaining HTML:

 <div ng-app="productManagement" class="container">
        <div class="panel panel-primary">
            <div class="panel-heading"
                 style="font-size:large">Product List</div>

            <div class="panel-body">
                <table class="table" ng-controller="ProductListCtrl as vm">
                    <thead>
                    <tr>
                        <td>
                            <button type="button"
                                    class="btn btn-primary"
                                    ng-click="vm.toggleImage()">
                                {{vm.showImage ? "Hide" : "Show"}} Image
                            </button>
                        </td>
                        <td>Product</td>
                        <td>Code</td>
                        <td>Available</td>
                        <td>Price</td>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="product in vm.products">
                        <td>
                            <img ng-if="vm.showImage"
                                    ng-src="{{product.imageUrl}}"
                                    style="width:50px;margin:2px"
                                    title="{{product.productName}}">
                        </td>
                        <td>{{ product.productName}}</td>
                        <td>{{ product.productCode }}</td>
                        <td>{{ product.releaseDate }}</td>
                        <td>{{ product.price | currency }}</td>
                    </tr>
                    </tbody>
            </table>
            </div>
        </div>
    </div>

Answer №1

There seems to be a spelling mistake here - the ProductResource-factory is defined as .factory("productResource") with a lowercase p:

(function () {
    "use strict";
    angular
        .module("productManagement")
        .controller("ProductListCtrl",
                    ["ProductResource",
                    ProductListCtrl]);

    function ProductListCtrl(productResource) {
        var vm = this;

        productResource.query(function (data) {
            vm.products = data;
        });

        vm.showImage = false;

        vm.toggleImage = function () {
            vm.showImage = !vm.showImage;
        }
    }
}());

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

I keep encountering an issue where I am receiving a TypeError stating that I cannot read the property 'now' of undefined

What could be causing the error I am experiencing? Here is the relevant code snippet: this.state = { now: 0 } setInterval(function () { this.setState({ now: this.state.now + 1}); }, 100); I am attempting to increment the 'now' value ...

Issue with Bootstrap 5: Mobile Menu Failure to Expand

I’ve been struggling with this issue for days now. I’ve searched everywhere for a solution, but to no avail. That’s why I’m turning to the experts here for help :-) The problem I’m facing is that my mobile menu won’t open. Nothing happens when ...

Would it be considered improper to apply ID's to DOM elements for the purpose of automation, as it may transform their CSS classes into individual entities?

Just thinking about the best approach for Automation (such as Selenium). I've been advised against using IDs on elements, as it could result in JS errors (in case of duplicate IDs) and make CSS classes unique. While I see the point, not having IDs can ...

What is the best way to add this dependency to an AngularJS application?

I am struggling to properly implement the injection of this dependency: https://cdnjs.cloudflare.com/ajax/libs/angularjs-dropdown-multiselect/2.0.0-beta.10/src/angularjs-dropdown-multiselect.js within my project. This is the current dependency injection s ...

JavaScript button mouse enter

There seems to be an issue with this code. The button is not functioning properly after hovering over it, even though it was copied from the instructional website where it works fine. <html> <head> <title>Button Magic</t ...

Struggling with NodeJs POST request issue

Let's imagine I have a specific URL https://exampleAPI.com/authorize?param1=value1&param2=value2 Where param1 and param2 are the payload values being sent. How can I execute a POST request in this particular way? I attempted the following: var ...

Adjust the TextArea content according to the quantity of selections made in the listbox

If I have a listbox alongside a textarea: <textarea id="MyTextArea"></textarea> <select id="SelectList" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="me ...

Preventing Ng-repeat from refreshing after deleting using $http request

Once I remove an item from my list, the deleted object's data disappears, but the placeholder (empty row) lingers. (I tried using apply() with no luck) I have a standard CRUD interface displaying expenses. <table class="table table-striped"> ...

Create an event listener for clicking on an image that potentially has a corner ribbon obscuring a portion of it

In my project, there is a div element with an id of items containing several bootstrap cards. Each card includes an image tag and some are accompanied by the following HTML code: <div class="ribbon"><span>Sale</span></div> This co ...

Is there a way to use JavaScript to choose options within a <select> element without deselecting options that are disabled?

Here's the code snippet I am working with at the moment: <select id="idsite" name="sites-list" size="10" multiple style="width:100px;"> <option value="1" disabled>SITE</option> ...

Filtering a list in AngularJS based on user input

Having an issue with this code. It's not working, but it works in another project. The code involves filtering a table using an input field. <md-content class="md-padding"> <br> <md-input-container> <label>sear ...

Using jQuery to dynamically attach events to elements in a webpage

I am currently working with some ajax functionality. When a successful ajax request is made, additional content is dynamically added to the page. However, I am facing an issue where I cannot attach events to these dynamically added elements. The process i ...

Deactivate the form outside of normal business hours

I need to create a form for a delivery service that only operates from 9am to 9pm. If a user submits the form outside of these hours, I want to redirect them to a page displaying the company's operating hours instead of a thank you page. For instance ...

gulp-webpack encountered an error: The entry module could not be found. Error: module resolution failed

Whenever I attempt to run gulp scripts, it gives me the following error message: ERROR: Entry module not found Error: Unable to resolve 'C:/Users/milan/Local Sites/project-university/app/public/wp-content/themes/fictional-university-theme/js/scrip ...

JavaScript allows you to merge two attributes of an object together

I currently have an object array stored in JavaScript. Here's an example: objArr = [{"FirstName":"John","LastName":"Doe","Age":35},{"FirstName":"Jane","LastName":"Doe","Age":32}] My goal is to transform this object array into a new format like the f ...

Using the $.ajax() method can sometimes lead to encountering the error message "Uncaught ReferenceError: data is not defined"

I experimented with different methods to retrieve .json files and data using $.getJSON and $.ajax() here The issue I encountered with my JS code number 2 is: $.ajax({ type: "GET", url: 'js/main.js', data: data, success: 1, }).done(fun ...

Cameras within the boundary of an A-frame restricted by distance

I'm trying to implement a code that restricts the camera movement in A-frame. The current functionality teleports the camera back to the position 0, 1.6, 0 when it moves 10 spaces away from the starting point on the x or y axis. However, I want to mod ...

Tips for modifying the color of a query term in HTML

Within my HTML file, I have <p class = "result"> {{searchResult}} </p> where {{searchResult}} represents the result of a search term. If I search for the term "hot", {{searchResult}} would display a string containing the word "hot" in a ...

While creating a customer profile sample, I encountered an issue where I received an error message stating "POST http://localhost:3000/cusprofiles/add 400 (Bad Request)" when attempting to add data

I'm having trouble creating a customer profile for a vet clinic in my code. I'm unable to add and edit customer details, and I'm not sure where I've made a mistake. The only method that seems to be working is the GET method. Can someone ...

The problem arises when the type of a Typescript literal union becomes more specific within React children

Currently, I am in the process of converting our React/Redux project to TypeScript and encountering a challenge with TypeScript literal type union types. The issue that I'm facing is as follows: I have instantiated a Wrapper component with a type pr ...