Basic angular service malfunctioning

I've been trying to create an angular service but seem to be having trouble with it. I've attempted various solutions and searched extensively for answers. Any assistance would be greatly appreciated.

 //service 

angular
    .module('RDash')
    .factory('googleLogin', googleLogin); 

function googleLogin() 
{
    this.testFunc = function () 
    {
        console.log("THIS IS A TEST SERVICE");
    }
};

Below: Attempting to call the testFunc from the service

//controller

angular
    .module('RDash')
    .controller('ComposeCtrl', ['$scope','$rootScope','$http','googleLogin', ComposeCtrl]);

function ComposeCtrl($scope, $rootScope, $http, googleLogin) {
    console.log("ComposeCTRL active");

    googleLogin.testFunc(); // encountering an error stating "main.min.js:2 Error: [$injector:undef] http://errors.angularjs.org/1.5.8/$injector/undef?p0=googleLogin" 

I suspect the issue lies in injecting the service, but I'm not quite sure where. Any insights or guidance would be much appreciated. Thank you.

Answer №1

When setting up a factory in Angular, it is important to return the function reference and declare a variable testFunc. To understand why, you can find the answer here.

Make sure to update the working snippet below.

angular.module('RDash', []);
angular
  .module('RDash')
  .factory('googleLogin', googleLogin);

function googleLogin() {
  var testFunc = function() {
    console.log("THIS IS A TEST SERVICE");
  }
  return {
    testFunc : testFunc
  }
};

angular
  .module('RDash')
  .controller('ComposeCtrl', ['$scope', '$rootScope', '$http', 'googleLogin', ComposeCtrl]);

function ComposeCtrl($scope, $rootScope, $http, googleLogin) {
  console.log("ComposeCTRL active");

  googleLogin.testFunc();
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="RDash" ng-controller="ComposeCtrl"></div>

angular
   .module('RDash')
   .factory('googleLogin', googleLogin);

 function googleLogin() {

   var testFunc = function() {
     console.log("THIS IS A TEST SERVICE");
   }

   return {
     testFunc: testFunc
   }

 };

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

Searching for a particular Prettier setting

Seeking a Nicer alternative. I am exploring if there is a way to format code like this without moving the first attribute to a new line: <div class="test-class" [test-binding]="true" (test-binging)="test()" ...

What is the reason for the text not being written continuously in the textfield?

Looking to create a page for collecting user information. This is a Codesandbox.io page where the issue arises. https://codesandbox.io/s/material-demo-z1x3q?fontsize=14 When I try to input "d" continuously in the 성별* textfield, I can only enter "d" ...

Different Option for Single-spa

We currently possess a vast enterprise application developed in angularjs. However, we are now faced with the task of transitioning to angular. Consequently, we have dismissed the option of employing the recommended "ngUpgrade" hybrid approach. Hence, we ...

Can I use the mouseover event on an image to display a sibling div?

Hi everyone! I'm fairly new to web development and I could really use some advice. I'm trying to implement a mouseenter event on an img tag to show a sibling div, but I'm encountering some strange behavior. The mouseenter event seems to be w ...

I'm encountering an issue with VS Code where it is unable to identify ejs tags within my project

I'm running into an issue with Vs code where it's not recognizing ejs output tags when they're inside an html tag, like in the input tag below : <input type="checkbox" name="checkbox" value="<%=item._id%>" onChange="this.form. ...

"What's the best way to update the src attribute of an iFrame when a user clicks

I'm new to coding in PHP or JavaScript, so please bear with me if my question isn't quite right. I'm looking for a way to dynamically pass and embed a URL from a link into the src attribute of an iframe. Essentially, I want users to click o ...

TinyMCE is deleting Bold tags in a numbered list when saving a form

I recently integrated the tinymce editor into one of my textareas, allowing users to format their text with options like bold, underline, and italic. Here is the implementation code: tinymce.init({ branding: false, statusbar: false, resize:true ...

JavaScript - Image style alterations are not operating as expected

Let me break it down for you: onmouseover="imageOn(bg-index);" onmouseout="imageOff(bg-index);" I've got these two attributes set on a table tagged with ID table-title. These functions are included in an external JS file: If the name is 'bg-i ...

What is the purpose of using the variable "header_row || 1" in App Script?

Recently, I stumbled upon a spreadsheet that contains app script for gathering keys of data requested through doGet. In the code, there is a line that reads like this: var headRow = e.parameter.header_row || 1; What exactly does this line mean? I searc ...

Reload the page when the value reaches 1

Hello, I am currently working on creating a system where my index page refreshes when a value in my database is set to 1. However, I am having trouble with the code as only my index.php is not refreshing. index.php <script> interval_timer = setInt ...

How is it that my initial response appears correct in the first log, but then suddenly changes to a 1?

I've encountered a strange issue where the response appears correctly in the initial log, but later changes to a '1' when console.log(data); is called. The screenshot illustrates the pattern: https://i.sstatic.net/zaXcg.png If you expand ...

Is it true that URL parameters can be found in two distinct locations within this.props in react-router?

<Route path="lookbook" component={Photos} onEnter={onPageEnter}> <Route path=":photoIdentifier" component={PhotoDetailsModal} onEnter={onPageEnter}> </Route> </Route> While in the PhotoDetailsModal, I used console.log to check ...

Convert JSON data into HTML using Angular.js's ng-shodown directive

Can someone help me understand how to properly use the makeHtml() function in directives.js to convert JSON into HTML? I've been studying the documentation thoroughly but I'm still confused. My project is built on angular.js 1.6.4 and I have inc ...

Consistently navigating back to the Home Page through AJAX

Can anyone assist me in resolving my issue? I have three files: index.php, process.js, and redirect_process.php. The index.php serves as my homepage with a form embedded in it. Whenever the submit button is clicked, the process.js script is triggered to v ...

After the initial rendering, JQuery can dynamically search through the DOM elements and seamlessly replace keys with their respective values

I am in the process of implementing my personal localization method on my web application that is currently under development. With the use of JQuery 2.2.0 and no other frameworks or third-party tools, I need to embed certain expressions directly into my H ...

JQuery horizontal navbar hover animations

Looking to design a simple navigation bar that displays a submenu when hovering over a link. The issue I'm facing is that the submenu disappears when moving from the link to the submenu itself, which is not the desired behavior. How can this be fixed ...

When attempting to install font-awesome with meteor npm, the module 'fontawesome'" was not found

Currently working with meteor version 1.4.1.1 which has NPM support enabled. I encountered an issue after installing the npm package "font-awesome" where the console displayed an error message stating "Uncaught Error: Cannot find module 'fontawesome&a ...

javascript extract data from JSON

How can I extract values from the [object Object] in javascript? I have a JSON response from PHP that I am passing into JavaScript. I want to retrieve the GPSPoint_lat and GPSPoint_lon values. var jArray = ; var obj = JSON.parse(jArray); I am gett ...

Use angular-resource to add a new entry to the db.json file

I have a JSON file called db.json with the following structure: { "menu":[ { "id":0, "item":"item1" },{ "id":1, "item":"item2" },{ "id":2, "item":"item3" } ], "feedback":[] } Currently, I am using Angular's $resource to send a Jav ...

Effective ways to conduct Protractor testing on angular-ui-Selectize components

I am exploring how to test a user creation form that includes multiple dropdown controls, specifically angular-ui-select elements. After searching for documentation on selecting items from these dropdowns, I came up empty-handed. This is an excerpt from ...