Is there a way to invoke a function within an Angular Service from within the same service itself?

Below is the code snippet I am working with:

angular.module('admin')

    .factory('gridService',

    ['$resource', 'gridSelectService', 'localStorageService',

    function ($resource, gridSelectService, localStorageService) {

        var factory = {
            gridSetup: function ($scope) {            
                $scope.editRow = function (row, entityType) {             
                    window.setTimeout(function () {
                        window.setInterval(function () {
                            submitItem($scope, $scope.modal.data);
                        }, 1 * 60 * 1000);
                        submitItem($scope, $scope.modal.data);
                    }, 1 * 60 * 1000);
                }          
            submitItem: function ($scope, formData) {

            }
         }
    }

I am trying to call the submitItem function from inside the window.setTimeout and setInterval, but it's not recognizing the function. How can I properly call it? I have tried using gridService.submitItem but that didn't work either.

Answer №1

To achieve the desired result, it is recommended to utilize 'factory.submitItem' in place of 'submitItem' exclusively.

angular.module('admin')

    .factory('gridService',

    ['$resource', 'gridSelectService', 'localStorageService',

    function ($resource, gridSelectService, localStorageService) {

        var factory = {
            gridSetup: function ($scope) {            
                $scope.editRow = function (row, entityType) {             
                    window.setTimeout(function () {
                        window.setInterval(function () {
                            factory.submitItem($scope, $scope.modal.data);
                        }, 1 * 60 * 1000);
                        factory.submitItem($scope, $scope.modal.data);
                    }, 1 * 60 * 1000);
                }          
            submitItem: function ($scope, formData) {

            }
         }
    }

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

What is the proper way to reference an EJS function that is declared in a different EJS file?

document 1: jsfunction.ejs <% function testFunction() {return 42;} %> file 2: data.ejs <% include jsfunction.ejs %> <% testFunction(); %> result: ReferenceError: 1| <% include jsfunction.ejs %> >> 2| <% testFuncti ...

Steps to avoid the button being submitted twice

I am facing an issue with two buttons in my code. One button is used to increase a count and the other button is meant to submit the count and move to the next page. The problem is that when I click on the "Proceed" button, it requires two clicks to procee ...

Looking for tags similar to stackoverflow?

Is there a way to create a search box similar to the one in Tags where tag names are displayed immediately upon entering without pressing enter key? Could anyone provide me with a script or tutorial on how to achieve this? Is it done using JavaScript or j ...

The issue persists with the addEventListener function not working multiple times

My issue is that the addEventListener function is only working for the 'scroll' event and not for 'addMoreFields' or 'removeFields'. If I move the scroll section down, then it works for 'addMoreFields' and 'remo ...

Using <Redirect/> in ReactJS results in rendering of a blank page

Hello everyone, I've been working on a feature where I want to redirect the user to the home page using <Redirect/> from react-router after they have successfully logged in. However, I'm facing an issue where the timeout is functioning corr ...

What is the best way to determine if an AJAX response is of the JavaScript content type?

There are multiple forms in my system, each returning different types of data. I need to be able to distinguish between a simple string (to display in the error/status area) and JavaScript code that needs to be executed. Currently, this is how I'm ha ...

Unexpected lag causing delays in jQuery animations

I am attempting to implement a "hover" effect using jQuery. Everything seems to be working fine, except for a strange delay that occurs only the first time the complete callback is triggered - oddly enough, this is the only instance where it reaches the pr ...

What is the best way to streamline a state-dependent getter, with a focus on adhering to SOLID principles?

Is there a more user-friendly way to retrieve different data based on the type in my Angular component? I'm considering separating the component into two: one for phone and one for email. However, I'm concerned about duplicating most of the logi ...

In Typescript, we can streamline this code by assigning a default value of `true` to `this.active` if `data.active

I am curious if there is a better way to write the statement mentioned in the title. Could it be improved with this.active = data.active || true? ...

Empty initial value of a number type input element in React using TSX

In the process of developing a react POS app using Typescript, I encountered an issue with calculating change when entering the amount of money received from a buyer. The problem arises when the first value passed to the change calculation logic is empty, ...

The error occurred while attempting to save the file to disk: 'setHeader() requires both a name and a value to be set.'

I am working on enabling image file uploads to the Node.js server in a MEAN Stack application. Utilizing ng-file-upload for the client-side angular directive has been successful so far. However, I have encountered an issue when attempting to pass the image ...

Development with Node JS, express, Mongoose, and intricate nested queries

I'm struggling with a group of interconnected queries using express/mongoose, structured like this: app.get(..., function(...) { Schema1.query(..., function(..., res1) { for ( var key in res1 ) { Schema2.query(..., function(..., ...

How to resolve CORS issues in an AngularJS, Spring Boot, and Nginx setup

For the past few days, I've been struggling with an issue. I have a backend server set up using Spring-boot with Rest API. This server is being called from a frontend interface built with AngularJS and managed by Nginx. Everything is running locally ...

What is the recommended depth in the call stack to utilize the await keyword for an asynchronous function?

My knowledge of async functions in TypeScript/React is fairly basic. I have two API calls that need to be made, and I am using async functions to call them from my UI. It's crucial for these calls to have completed before rendering the component corre ...

Highlight text when the user is scrolling the page - using JQUERY

Can anyone suggest a way to dynamically underline text as the user scrolls down a webpage? The underline should only be visible while the user is scrolling. I've experimented with animate.css and other plugins, but haven't been able to achieve th ...

What happens when you click and trigger mouseleave?

window.onload = function() { $(".compartir").hover(function() { console.log('hover'); var self = this; setTimeout($(self).addClass('ready'), 500); }, function() { var self = this; console.log('leave'); ...

"Exploring the process of making a REST call from an Angular TypeScript client to

I'm currently developing a Sessions Server for a project at work. My dilemma lies in the fact that I'm struggling to find resources on how to make JavaScript HTTP calls from a server running with http.createServer() and server.listen(8080, ...) ...

The webpage displays the element as <span class="icon">&#xe80d;</span> instead of rendering it properly

In my ReactJS project, I have created a component called Menu1item: class Menu1item extends React.Component{ render(){ return ( <div> {this.props.glyph}{this.props.value} </div> ) ...

Why doesn't package.json typically utilize the preset values stored in the .npmrc file?

Windows 10 x64 Based on the information found here, I created a file called C:\Users\bushm\.npmrc with the following content: author = "Andrey Bushman" However, when I run the command npm init -y in a new directory, I noticed that the pac ...

Allow users to edit the textarea only to input values, not from

I am trying to achieve a specific functionality with two input fields and one textarea. Whenever I type something in the input fields, their values are automatically populated in the textarea using .val(). Currently, this feature is working as intended, b ...