Oops! Looks like there was a problem with the product resource provider in the ProductListCtrl

https://i.sstatic.net/TxQSt.png

Being new to angularjs, I'm facing an error that I can't quite figure out. My goal is to consume a webapi service with the following code.

The app.js file:

(function () {
    "use strict";

    var app = angular.module("productManagement",
                            ["common.services"]);

}());

productListCtrl.js

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

    function ProductListCtrl(productResource) {
        var vm = this;

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

commonservices.js

(function () {
    "use strict";

    angular.module("common.services", ["ngResource"])
    .constant("appSettings", {
        serverPath: "http://localhost:55755/"
    });
}());

productResource.js

(function () {
    "use strict";

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

    function productResource($resource, appSettings) {
        return $resource(appSettings.serverPath + "/api/products/:id"); 
    }
});

Index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8>
    <title>Acme Product Management</title>

    <!-- Style sheets -->
    <link href="Content/bootstrap.css" rel="stylesheet" />
</head>

<body ng-app="productManagement">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <div class="navbar-brand">Acme Product Management</div>
            </div>
        </div>
    </nav>

    <div class="container">
        <div ng-include="'app/products/productListView.html'"></div>
    </div>

    <!-- Library Scripts -->
    <script src="scripts/angular.js"></script>
    <script src="Scripts/angular-resource.js"></script>
    <!-- Application Script -->
    <script src="app/app.js"></script>

    <!-- Services -->
    <script src="Common/common.services.js"></script>
    <script src="Common/productResource.js"></script> 
    <!-- Product Controllers -->
    <script src="app/products/productListCtrl.js"></script>
</body>

</html>

Answer №1

The issue at hand is the lack of execution of the anonymous function block defined in productResource.js, causing the failure to register the productResourceProvider:

Update as follows:

productResource.js

(function () {
    "use strict";

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

    function productResource($resource, appSettings) {
        return $resource(appSettings.serverPath + "/api/products/:id"); 
    }
}); // <-- The problem occurs here.

Modify to:

(function () {
    "use strict";

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

    function productResource($resource, appSettings) {
        return $resource(appSettings.serverPath + "/api/products/:id"); 
    }
}());  // <-- Ensure execution of the function block.

Answer №2

Arrange your JS file order correctly.

<script src="app/products/productListCtrl.js"></script>
<script src="Common/productResource.js"></script> 
<script src="Common/common.services.js"></script>
<script src="app/app.js"></script>

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

How can I dynamically append content to the DOM when a user clicks?

On my form, I have an input field that allows users to click an add button and a new input field will appear below it, with the option to repeat this process indefinitely. However, I am facing an issue with adding an input field to the DOM upon a click eve ...

Issue with e2e.js file format in Cypress Support

I am trying to save Cypress screenshots into a report using a support file as recommended in the documentation. However, I keep encountering an error: Your supportFile is missing or invalid: support/e2e.js The supportFile must be a .js, .ts, .coffee file ...

Uploading and previewing files in Angular

Using the ng-file-upload library, I am able to successfully post files to my backend Web Api. The files are saved in the folder: "~/App_Data/Tmp/FileUploads/" The file path is also stored in my database. When I enter edit mode, I want to display a previ ...

Please provide a valid C# identifier

For some reason, I'm encountering an issue when attempting to include a mailto tag around my gridview text - it's resulting in an Identifier expected error. if (GVStatus == "Team") { if (e.Row.RowType == DataControlRowType.D ...

Remove all members assigned to a specific role

I've been working on coding my discord bot, and I'm trying to implement a feature that kicks all users with the "Unverified" role. I know how to list users in a role and how to kick someone individually, but I can't figure out how to combine ...

How to Force Redirect to HTTPS on Elastic Beanstalk with Node.js and Express

Seeking assistance in setting up a site to enforce HTTPS (redirecting from HTTP). Our HTTPS is configured through AWS Elastic Beanstalk, but currently both HTTP and HTTPS are accessible. After reviewing various resources, such as this one, I attempted the ...

Error: Attempting to append a child to a non-existent property

I am currently learning Java Script and this is the code I have been working on. <!doctype html> <html> <head> <style> div {position:absolute; width:500px; height:500px} img {position:absolute} ...

What is the best way to transform a string into an XML object using JavaScript?

Even though I am aware of this question that already exists, unfortunately, it hasn't been helpful to me. In my application, I have a section that loads an actual XML document using the following function: jQuery.ajax({ type: "GET", url: fil ...

Iterate through a collection of files in an asynchronous manner by leveraging promises and defer statements

I have a specific objective to navigate through a directory of files, perform certain operations on each file, and then produce a JSON object with a portion of the directory. Initially, I managed to achieve this using the synchronous functions in Node&apo ...

Issues with Javascript's JSON.parse functionalityI'm having trouble

As a complete newcomer to the world of JavaScript, I find myself struggling with some issues regarding JSON manipulation. This is my very first attempt at working with it, and I am encountering difficulties in converting my JSON string into an object that ...

What is the best way to retrieve results by passing an array in MongoDB?

I currently have an array that I am using to search for specific products. The array is displayed below. "productIds": [ { "productId": ObjectId("574592dfc07f13943255c19d") }, { "productId": ObjectId("5745934cc07f13943255c19f") }, ...

Find all the different ways that substrings can be combined in an array

If I have a string input such as "auto encoder" and an array of strings const arr = ['autoencoder', 'auto-encoder', 'autoencoder'] I am looking to find a way for the input string to match with all three values in the array. ...

Is there a way to prompt Vue Router to refresh my page while attempting to navigate within the present location?

I currently have my project structured in the following way: Views Home.vue Classroom.vue App.vue Within the Home.vue file, I display the content and a login form. Essentially, users must submit their login information, and if correct, the isAuth vari ...

Disabling a LinkButton within a GridViewclick using JavaScript on the Client Side

I am working with a GridView that includes a LinkButton control in one of the columns. I need to disable the click event from the client side based on certain conditions for this column. This means that for some rows, users should not be able to trigger th ...

Is there a way to retrieve the current route on a custom 404 page in Next.JS?

I have set up a custom 404 page for my Next.JS application (404.js). I want to display a message stating The route <strong>/not-a-route</strong> does not exist, but when I use Next.js's useRouter() and router.pathname, it incorrectly ident ...

How can I handle the issue of the body background overlapping with the modal window?

Recently, I created a dialog box using the code below: $(function(){ $('#saveandcontinue').click(function(){ $('body').css('background', '#999'); $('.footer').css('background&a ...

React is unable to identify the `initialValue` attribute on a DOM element

Warning: React is not able to recognize the `initialValue` property on a DOM element. If you intended for it to show up in the DOM as a custom attribute, use `initialvalue` in lowercase instead. If it was mistakenly passed from a parent component, make sur ...

Exploring the Power of Modules in NestJS

Having trouble with this error - anyone know why? [Nest] 556 - 2020-06-10 18:52:55 [ExceptionHandler] Nest can't resolve dependencies of the JwtService (?). Check that JWT_MODULE_OPTIONS at index [0] is available in the JwtModule context. Possib ...

Verify whether a particular font is installed on the system

Is there a way to use JavaScript or jQuery to check if a user has Proxima Nova font installed when using Typekit? If so, I would like to incorporate that feature and then utilize jQuery to remove the Typekit loading class in order to optimize page loading ...

Why does setting the variable value as an argument in an AngularJS function result in the variable becoming undefined?

The following is the Main Controller implementation: angular.module("HomePageApp", ["BaseApp"]) .controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) { var self = this; self.posts = BaseSer ...