Angular JS throwing an error: [$injector:unpr] Provider not recognized

I am new to angular js and I'm attempting to create a custom service. I've copied and pasted some code for the custom service, but I keep encountering the following error:

Error: [$injector:unpr] Unknown provider: serviceProvider <- service <-         MainController
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=serviceProvider%20%3C-  %20service%20%3C-%20MainController
at REGEX_STRING_REGEXP (angular.js:68)
at angular.js:4262
at Object.getService [as get] (angular.js:4409)
at angular.js:4267
at getService (angular.js:4409)
at Object.invoke (angular.js:4441)
at $get.extend.instance (angular.js:8999)
at nodeLinkFn (angular.js:8109)
at compositeLinkFn (angular.js:7541)
at publicLinkFn (angular.js:7416)(anonymous function) @ angular.js:12330

Here is the code for my custom service:

(function () {
    var myModule = angular.module('app', ['onsen']);
    myModule.service('service', ["$http", function ($http) {
        var test = function (usertoken) {
            alert("fffffffffffffffffffff");
        }
        return {
            getUserSites: getUserSites
        };
    }]);
})();

And here is my module implementation :

(function () {
    var myApp = angular.module('app', ['onsen']);

    var MainController = function ($scope, $http, $interval, service) {
        $scope.clciclme = function () {
            alert("HIIIIIIIIIIIIIIIIIIII");
        }
    }
    myApp.controller('MainController', MainController);
})();

I would appreciate any help in resolving this error. Thank you.

Answer №1

You're utilizing the module's setter syntax twice:

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

This leads to the creation of the app module twice. To switch to the getter syntax, omit the array parameter like so:

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

Your revised code would appear as follows:

Controller:

(function () {
    var myApp = angular.module('app', ['onsen']); // using setter syntax once

    var MainController = function ($scope, $http, $interval, service) {
        $scope.clciclme = function () {
            alert("HIIIIIIIIIIIIIIIIIIII");
        }
    }
    myApp.controller('MainController', MainController);
})();

Service:

(function () {
    var myModule = angular.module('app'); // using getter syntax for subsequent instances

    myModule.service('service', ["$http", function ($http) {
        var test = function (usertoken) {
            alert("fffffffffffffffffffff");

        }
        return {
            getUserSites: getUserSites
        };
    }]);
})();

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

My goal is to retrieve and print the duplicated values only once from an associative array

Given an associative array, I need to print all the department names without any repetitions. <h3>2. List out all department names</h3> <div class="all"> </div> Here is my JavaScript code: var employee=[{"firstName":"Zahir","last ...

Using AngularJS to implement validation on radio buttons

My application is a cross-platform app that utilizes AngularJS, Monaca, and Onsen UI. Within one of the views, there exists an array of list items where each item can be associated with a random number of radio buttons. These lists are dynamically generat ...

To activate a service worker in the SRC folder of your ReactJS project, you can simply register it in the index.html file

Implementing a Service Worker in Another Folder I am attempting to utilize a service worker to precache my entire PWA and I need to place the SW file inside the SRC folder. How can I run my service worker from public/index.html? I am using create-react- ...

I'm new to Bootstrap and I'm looking for a way to customize the functionality of table 1 compared to table

I am currently facing a unique issue for which I cannot find a solution. The problem lies in my timesheet table, where I intend to only add data to Table #1. However, whenever I click the + sign in the table, the same data is also displayed in Table #2. De ...

Exploring the power of Nestjs EventEmitter Module in combination with Serverless functions through the implementation of Async

I am working on implementing an asynchronous worker using a serverless lambda function with the assistance of the nestjs EventEmitter module. The handler is being triggered when an event is emitted, but the function closes before the async/await call. I ...

VueJS - Display a v-html tag that is initially empty but will eventually show content determined by

I have created a recursive component that is capable of infinitely looping over its children At the top of this component is the following line, which serves as the first element within the Vue component: <component class="relative " :is="type || &apo ...

Defining a Global Variable using the jQuery $(this)

I have been looking for ways to simplify my coding process, and one method I've tried is assigning a global variable. var parent = $(this).parent().parent().parent(); var parentModule = $(this).parent().parent().parent().parent(); Throughout my code ...

Enhance Your AG-Grid Experience with a Personalized Colorizing Pipe

I'm having an issue with my AG-Grid implementation in my app. I'm trying to use a custom color pipe that I created in one of the columns using cellRenderer. However, I'm encountering an error and I'm not sure how to fix it. Below is my ...

How can I incorporate checkboxes and crosses for validation in AngularJS?

I've been searching through the documentation for angularjs and online resources, but I can't seem to find any information regarding a specific aspect of form validation. You know when you input something in a form field (like a name, phone numbe ...

Is there a way to trigger a function in an AngularJS controller from a Backbone controller?

I've been working on an application that was originally developed using backbone and jQuery, but we had to incorporate new modules built with angular to meet client requirements. Routing in the application is handled by backbone route, and we have suc ...

Tips for creating a vertical wave animation of a ribbon using svg

I have an SVG code for a vertical ribbon that I want to add a wave effect to. The wave should start from the top and flow continuously to the bottom, but currently it's not working as expected. @keyframes thread{ from { stroke-dashoffse ...

Expanding the functionality of the Ember JSONAPIAdapter for retrieving JSON data from a specified URL

As a newcomer to Ember.js, I am currently delving into understanding how Ember works. One issue I have encountered is calling my Django API from an Ember.js route using the following code: this.store.findAll('MYMODEL', 'ANOTHER_MODEL_ID&apos ...

The timestamp within Javascript setInterval remains static without updates

Recently, I've been experimenting with using setInterval in my Javascript code. As a quick test, I attempted to create a live updating clock display. var tim = new Date(); function loadLog(){ document.getElementById('timebox').innerHTML ...

Obtaining the value of a specific dynamic `<td>` element using jQuery

My page layout resembles this design (I am unable to post an image due to insufficient points, so I have it hosted elsewhere) My goal is for jQuery to identify the specific email when a user clicks on the delete button. For instance, if a user clicks on & ...

Adding fresh data to a C3.js line graph

I am attempting to dynamically update a C3.js line chart using a function that retrieves a set of x and y values from a database. The function I want to use for inserting the data is: function insertGraph(yAxis, xAxis, Header) { setTimeout(function () ...

The latest alpha version of Angular2 Material Design (alpha.9-3) encountered a "404 not found" error when trying to access @angular

After carefully following the steps outlined in the angular material2 Getting Started guide to install @angular/material, I made updates to package.json, app.module, and systemjs.config using Atom. Specifically, I added the line '@angular/material&apo ...

Ways to instruct tween.js to pause until a texture is shown in three.js

I encountered an issue while using three.js alongside tween.js. The problem arises when the timer for a tween.js animation begins before the texture of a sphere is actually displayed. Currently, I am developing a panorama viewer with three.js specifically ...

How can eval() be effectively replaced in JavaScript?

Having trouble with javascript eval()? I've encountered bugs and decided to replace all instances of it. Here's an example of what I've changed: var abc = "item." + str3 + ".power"; abc = eval(abc); Now, it's been updated to: var abc ...

What is the best way to display flash messages within a Bootstrap modal dialog box?

I am facing an issue with my bootstrap modal window and form. I use flask.flash() to display error messages, but when I click upload for the first time, the modal window closes. The error message only shows up when I reopen the modal window. I want the err ...

Enable users to choose either today's date or a future date by implementing AngularJS UI Bootstrap

I am currently utilizing the ui-bootstrap directive for a datepicker. My goal is to restrict the selection of dates to today's date or any future dates. Below is the HTML code snippet for the datepicker: <div class="input-group"> ...