Issue: $injector:unpr Unknown Provider (app.js is appearing to be properly defined)

Struggling with the unknown provider issue, I've searched through other threads and tried their solutions to no avail. My goal is to inject 'MockSvc' service into a controller without encountering any errors. Any advice would be greatly appreciated.

app.js:

(function(){

    'use strict';
    console.log('app.js initialized....');
    var app = angular.module('app', ['ngRoute']);

})();

service:

(function(){

    'use strict';
    var app = angular.module('app');
    app.factory('MockSvc', MockSvc);

    console.log('MockSvc service initialized...');

    function MockSvc(){

        var service = {

            };

            initialize();

            return service;

            function initialize() {

                console.log('MockSvc function initialization...');
            };


    }

})();

controller:

(function(){

    'use strict';

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

    app.controller('PONumSearch2', PONumSearch);

    PONumSearch.$inject = ['$scope', '$http', '$cookies', '$cookieStore', '$location', '$window','MockSvc'];


    function PONumSearch($scope, $http, $cookies, $cookieStore, $location, $window,MockSvc){ 

//controller logic goes here....

})();

Answer №1

When working with controllers, there is no need to inject dependencies directly. Injecting dependencies will create a new module instance each time.

To make changes:

Instead of

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

Use

  var app = angular.module('app');

Add ngCookies as an injected dependency in your main module,

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

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

Deploying an Azure Blob Trigger in TypeScript does not initiate the trigger

After successfully testing my Azure function locally, I deployed it only to find that it fails to trigger when a file is uploaded to the video-temp container. { "bindings": [ { "name": "myBlob", "type&qu ...

Using command line arguments to pass parameters to package.json

"scripts": { "start": "gulp", ... }, I have a specific npm package that I'm using which requires passing parameters to the start command. Can anyone help me with how to pass these parameters in the command line? For example, is it possible ...

Updating specific data in MongoDB arrays: A step-by-step guide

{ "_id":{"$oid":"5f5287db8c4dbe22383eca58"}, "__v":0, "createdAt":{"$date":"2020-09-12T11:35:45.965Z"}, "data":["Buy RAM","Money buys freedom"], & ...

Exploring the power of a "resolution" in ui-router

I am in a situation where I have two states that require the same resolve functionality: .state('state', { url: '/somewhere', templateUrl: '/views/somewhere.html', controller: 'myController', resolve: { ...

Tips on how to implement dropdownlist filtering with angularJs based on the selection from another dropdownlist

There are two dropdown lists in my code: <div class="control-group"> @Html.LabelFor(x => x.ServiceId) @Html.DropDownListFor(x => x.ServiceId, new SelectList(Model.ServiceList, "Id", "Title"), new { @class = "open" }) &l ...

Changing a single state in React results in the modification of both states simultaneously

Whenever I attempt to modify one state, I find that another state is inexplicably changing as well. I've scoured my code for the issue but can't seem to pinpoint it. What steps should I take next? Here's the snippet of code in question: impo ...

Failure to display updated property value

After rendering an array of objects, I am attempting to add a new property using a function. However, the new property value is not displaying on the page even though it is present when I log the object in the console. The new property that I want to add ...

The TransferList component in Material UI does not update its state based on props when using the useState

My TransferList component in Material UI receives an array of previously selected items as props.selectedItems. The props.Items contains all available items. I expected to see props.selectedItems in the left panel of TransferList, but the left panel is em ...

jQuery seems to have difficulty selecting the text of a hyperlink's element using the text() or html() methods

I have a <a href=#>Title</a> link and it's the content in between <a href="#"> attribute that I am trying to target but haven't been successful using either the text() or html() jQuery functions. The text() method is returning ...

The request to the route timed out after waiting 5000ms for a response from the server

I am a newcomer to using Cypress and I'm exploring an HTML page for testing purposes. My goal is to test the login authentication and log the body of an XHR. Here's the test code I wrote for this: describe('Login test', function () { ...

Saving an Image from HTML5 <canvas> using Java Servlet

Despite the numerous StackOverflow questions on this topic, I have gone through many of them without success. Now, I am reaching out with my own question. My objective is to save an image from an HTML5 <canvas> on my webpage to a file on my server u ...

Resetting React state can occur during routing in Ionic applications

I've been working on implementing React states in a way that allows all components and pages to easily access important variables with updates reflected across the app. However, I've encountered an issue where my state/context is reset to its ini ...

Issue with the loop function

When I try to loop through my code, I keep getting the same "y" value (5) and it doesn't change. What I actually want is to make the ajax call repeat X times [all], passing both the response and the current call number through an anonymous function. A ...

What is the best way to transfer a user-generated PNG file to my backend server?

I'm in the process of developing a website that enables users to generate personalized graphics and easily download them directly from the platform. My approach involves allowing users to customize an svg using a javascript-powered form, which is the ...

Troubleshooting the issue of array filtering not functioning properly in Async.js

When attempting to utilize async's filter method, I am not receiving the expected result. Could someone point me in the right direction? async.filter([1, 3, 5], function (item, done) { done(item > 1); }, function (results) { con ...

What is the most efficient way to update a specific element in a redux/vuex store?

What is the most efficient way to update an element(hash) in a list within a store (redux, vuex) using O(1) complexity? The order of the elements must be maintained as I will be adding/removing elements frequently. Updates will occur every millisecond, re ...

Executing a JavaScript Function in the Background using Cordova

Despite the numerous questions and plugins available on this topic, finding a solution has proven to be elusive for me. The most highly recommended plugin for this issue can be found here. My goal is to run MyService in the background, subscribe to the ON ...

Integrate the form outcome into the search results organized by book titles

There is a search form for finding books on the page, but I am having trouble adding the result of a particular book to the $scope.books object. Can you help me with this? Here's the code snippet: https://jsfiddle.net/dbxtcw9w/2 Thank you for your ...

Dynamically allocate a controller to an element following the Bootstrap process

I have a unique AngularJS application that is initialized manually at a specific time. Later on, I need to display an HTML element without any ng-controller assigned to it. Is there a way to dynamically add an ng-controller to this element in order for it ...

When an input is disabled in "react-hook-form", it may return undefined

Within my React application, there exists a form containing various input fields. I have enclosed these fields using FormProvider imported from react-hook-form and utilized register within each field. import { useForm, FormProvider, useFormContext } from & ...