Saving a value in a service using AngularJS

I am facing an issue with passing a variable into a service that needs to be accessed from multiple states within my application. I have created a service and attempted to pass a variable from a controller into the service in order to access it from another state and utilize it in other functions as well.

My code snippet is as follows:

The controller receives input from an HTML form and uses it in a function. I also aim to store the value obtained from the input inside a service for further use in other functions.

$scope.submit = function()
{
    var input = document.getElementById('userCode').value;
    var current;
    for(var i = 0; i < json.details.length; i++){
        current = json.details[i];
        if(current.pin == input){
        $state.go('LogInHome');
        $scope.Current = currentUser;
        $scope.Current.currentUser = input;
        }
    }
    console.log(current.currentUser);
}

Below is the code for the service:

App.service('currentUser', function () 
{   
return{};
});

Upon checking the console, I notice that I am getting an "Undefined" output. Can anyone point out where I might be going wrong?

Answer №1

$scope.login = function()
{
var userInput = document.getElementById('userInput').value;
var user;
for(var i = 0; i < userJson.data.length; i++){
    user = userJson.data[i];
    if(user.pin === userInput){
    $state.go('Homepage');
    $scope.currentUser = user;
    $scope.currentUser.pin = userInput;
    user = $scope.currentUser;
    }
}
console.log(user.pin);
}

Hopefully this code snippet helps you with your login process. Let me know if you have any questions!

Answer №2

Oops, looks like I made a silly mistake. Instead of deleting this and pretending it never happened, I'm owning up to it and sharing with you all. I forgot to inject the service. I apologize for any inconvenience this may have caused.

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

Submit a data array using formData through axios

I am planning to send array data using formData. The backend is set up to accept the data array separated by a dash ; For example, if I were to use Postman and input the form-data like this: id_barang : 122;288;383 (sending 3 values of id with dashes ;) W ...

What is the output of an Angular Js factory?

I'm struggling to wrap my head around how a factory can return an object of itself. Take this example: var myapp = angular.module('myapp',[]); myapp.factory('myfactory',function(){ var foo = {}; factory.message = functio ...

Guide on sending a message to a specific channel using Discord.js version 13 with TypeScript

After recently diving into TypeScript and seeing that Discord.js has made the move to v13, I have encountered an issue with sending messages to a specific channel using a Channel ID. Below is the code snippet I am currently using: // Define Channel ID cons ...

determine if the req.params parameter is void on an express route

Hi everyone, I'm a beginner with node and express and could really use some guidance. I am currently attempting to create a get request that checks if the req.params.address_line is empty, and then performs an action based on that condition. However, ...

Dealing with textarea in Javascript

I am new to JavaScript and facing a challenge in creating a delimited string from a textarea input. The issue is that when the textarea is passed in, it includes newlines for each row. I aim to parse the entire textarea content into a string with a delimit ...

Looking for a method to substitute "test" with a different value

Showing a section of the menu using <li id="userInfo" role="presentation" data-toggle="tab" class="dropdown"> <a href="#" name="usernameMenu" class="dropdown-toggle" data-toggle="dropdown" role="button"> <span class="glyphicon glyph ...

"Is it possible to rearrange the parent div's position when hovering over a child image with jquery sortable

Can you assist me with a task? I have two text boxes with an equal sign image, and I want the user to be able to move the div only when hovering over the equal sign. This means that the user should be able to drag the div using the equal sign. Can you hel ...

I have added the same directive to both of my HTML files

Hello, I am facing an issue with a directive that I have included in two HTML files. The 'app' is set as ng-app. <dir auto=""></div> The code for the directive is as follows: app.directive("auto", function() { scope: { arr : ...

performing functions concurrently within angularjs

I am currently utilizing angularjs 1.0 within my application. There is a dropdown on my cshtml page <select tabindex="2" id="Employee" ng-model="models.SelectedEmployee" ng-change="changeEmployee()" disabled="disabled" class="Answer" size="6"> < ...

Exploring the World of Subclassing Arrays in JavaScript: Uncovering the TypeError of Array.prototype.toString's

Can JavaScript Arrays be subclassed and inherited from? I am interested in creating my own custom Array object that not only has all the features of a regular Array but also contains additional properties. My intention is to use myobj instanceof CustomArr ...

What steps can be taken to address issues with the counting system in a lootbox opening

I am currently developing a virtual loot box simulator and have encountered a challenging issue. My goal is to track the quantity of Normals, Legendaries, Epics, and Rares obtained by the user. However, I am facing a problem where instead of updating the c ...

A recursive approach to filling empty space on a canvas using Javascript

My goal was to develop a recursive function that would continue filling the empty spaces of a canvas until reaching the edges or encountering a different colored space. function createFillingPoint() { let x = points[0][0], y = points[0][1]; var pat ...

AngularJS $http.post() response function not executing in the correct sequence

When calling a function from my angular controller to make a $http.post() request, the code below the function call is executing before the successFunction(), preventing the code inside the if block from running. How can I ensure the if block executes wi ...

Using anti-cache code in jQuery's getJson function

During an interview, I was presented with the following question: Can you provide a one-liner code showing the proper syntax to add an anti-cache code to all jQuery getJson calls in a project without individually adding it to each call? I must admit th ...

Unable to retrieve the status for the specified ID through the use of AJAX

When I click the button in my app, I want to see the order status. However, currently all statuses are being displayed in the first order row. PHP: <?php $query = mysqli_query($conn, "SELECT o.orderid,o.product,o.ddate,o.pdate,k.kalibariname,o.sta ...

Can the CSS color of struts2-jquery-grid-tags be modified?

Is there a way to customize the CSS style of my Struts2 jQuery grid tags? I'm having trouble adjusting the font size of the header layer. Can someone please advise on how to change the style, color, and other formatting of the grid similar to regular ...

Strategies for identifying specific children in my particular situation

Here's a snippet of my code: <ul id='nav'> <li><h1 id='unique' class='title'>Topic</h1></li> <li><h1 class='toptitle'>Department</h1></ ...

Troubleshooting JavaScript Functions Failure Following AJAX XML Request

My current project involves developing an interactive map where users can select a year from the timeline and apply filters. This functionality is achieved through XML HttpRequest, which refreshes the SVG each time a selection is made. The SVG code for th ...

Why isn't the Angular function being activated by the HTML generated by the datatable?

{ sTitle: "ANSWER_CORRECT", mData:"ANSWER_CORRECT", sClass: "readonly", mRender: function(data, type, obj) { ...

Exploring Fetch API with Promise.all

I have successfully implemented a functionality that fetches data from two URLs and performs an action only when both requests are successful. However, I am curious if there is a more efficient and succinct way to achieve the same result. Helper functions ...