Tips for accessing data from another service variable within an AngularJS service or controller

$http.get('/FetchData').then(function(response) {
$scope.newTimeStamp = response.data.result[1].timestamp;
    var timeValue = moment($scope.newTimeStamp,"YYYY-MM-DD HH:mm:ss");
        if($scope.newTimeStamp != undefined && $scope.newTimeStamp != ''){
            $scope.newTimeStamp = $scope.newTimeStamp;
        } else {
            $scope.newTimeStamp = '';
    }
}

Now, I need to pass this timeStamp value to a different page when making another service call. How can I achieve this and display it there?

I'm not very proficient in coding... Any help would be appreciated.

Answer №1

When it comes to sharing data across controllers in angular JS, you have a few options like localstorage, sessionstorage, or $rootScope.

One way to share data using localstorage is:

localStorage.setItem('title', $scope.title);
// Retrieve the title
$scope.title = localStorage.getItem('title'); 

For sessionstorage, you can use:

 sessionStorage.setItem(key, value);

 // To retrieve the data 

 sessionStorage.getItem(key, value);

To share data via $rootScope:

var app = angular.module("myApp", []);
      app.run(function($rootScope) {
         $rootScope.userData = {};
         $rootScope.userData.firstName = "Ravi";
         $rootScope.userData.lastName = "Sharma";
      });


      app.controller("firstController", function($scope, $rootScope) {
           console.log($rootScope.userData.firstName)
      });

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 best method for accessing a remote text file using JavaScript?

I have a text file located at \192.168.18.141\SnortLogs\alert_json.txt that I have shared using samba. I am able to access it through my Windows machine and now need to write JavaScript code to read the contents of this text file. ...

Retrieve the post ID from the existing URL via AJAX in order to fetch data associated with that particular ID

Currently, I am facing an issue while trying to pass the id from the current URL to the select.php page. The error displayed is 'id not defined' and as a result, no data is being fetched. My goal is to retrieve data for the suporder index from th ...

Is there a way to showcase the contents of the angular "tags" object seamlessly?

Is there a way to show the content of the "tags" object using angular? I attempted to achieve it using {{gallery.tags.tag}} but unfortunately, it did not work import {IPhoto} from "./iphoto"; export interface IGallery { galleryId: string; title: ...

Adjust font size based on screen dimensions

I am working on optimizing font sizes for different screen sizes in HTML elements. Currently, I use a script to dynamically adjust the font sizes based on window width, but it is making my code messy: <script> $(window).resize(function(){ $('#f ...

What is the method to retrieve the character set of response data in an Ajax request?

Ajax Request: $("button").click(function(){ $.ajax({url: "demo.html", success: function(result){ $("#div1").html(result); }}); }); In this code snippet, an Ajax request is made to fetch data from the demo.html file. The response data is s ...

Using jQuery to create sliding animations with left and right transitions

I recently learned about the slideUp and slideDown functions in jQuery. Are there any similar functions or methods for sliding elements to the left or right? ...

Is there a way to transform my drag-and-drop function into a click event instead?

I am currently contemplating a modification to my drag and drop game. The game involves a grid of words, with the highlighted word to be spelled. Traditionally, players would drag and drop letters to complete the word. However, I am now exploring the idea ...

ng-bind-html behaving unexpectedly

index.html <div ng-bind-html="htmlElement()"></div> app.js $scope.htmlElement = function(){ var html = '<input type="text" ng-model="myModel" />'; return $sce.trustAsHtml(html); } However, when attempting to retrieve t ...

Launching the Node.js Thread Pool to Enhance Parallel Processing

Hey there! I recently discovered that Node.js operates on a single thread. In order to optimize the performance of my application (MongoDB/Express), I've come up with the following script to utilize all 8 processors: #!/bin/bash node app.js & nod ...

Issue with Slider Width in WP 5.6 editor and ACF Pro causing layout problems

Is anyone else experiencing a specific issue after updating to WP 5.6? Since the update, all my websites are facing problems with rendering a Slick Slider in the Block Editor. Interestingly, everything looks fine on the front-end. The root of the problem ...

Using Redis for pub/sub functionality within or outside of the io.connect callback

Is it preferable to move the redis subscription event outside of the io.connect callback if the intention is to broadcast the data to all connected users? Or would it be better to keep it inside the io.connect callback, as shown below: io.on('con ...

Exploring the Scope of React Functional Components

My current dilemma involves a React Functional Component acting as a video player. I'm facing an issue where I need to invoke a function named onEnded when the video player triggers its ended event. This function takes in two parameters: a callback pa ...

AngularJS encountered an error: The token '|' was unexpected and expected ':' instead

My HTML file contains a condition where certain fields need to be displayed automatically in landscape mode using filters. However, when I run the program, I encounter the following code: <tbody> <tr ng-repeat="ledger in vm.ledgers ...

How can I use PHP to transform a JSON object containing xy coordinates into an image?

What is the best way to convert a JSON object containing xy coordinates into an image (PNG or JPG) using PHP or JavaScript? ...

Is there a more efficient method for generating dynamic variable names from an array aside from using eval or document?

I need help figuring out how to create an array in JavaScript or TypeScript that contains a list of environment names. I want to iterate over this array and use the values as variable names within a closure. My initial attempt looks like this (even though ...

What is the process for defining a filename when exporting with Webdatarocks default settings?

I'm having a hard time figuring out how to set a custom name for the exported file instead of just "Pivot". The HTML/Vue part contains the Pivot along with a select dropdown for filtering by date, which is working fine. The issue lies in customizing t ...

Mobile devices seem to be constantly refreshing website images

I have a landing page consisting of multiple sections with images as the background, each section occupying the full screen. In one specific section, the images change every 5 seconds. The website works smoothly on desktop, but I encounter issues on mobil ...

What is the best way to implement a loop using JQuery?

<script> $(function() { $('.slideshow').each(function(index, element) { $(element).crossSlide({ sleep: 2, fade: 1 }, [ { src: 'picture' + (index + 1) + '.jpg' } ]); }); ...

What methods can I use to distinguish between the use of a hardware keyboard and a soft keyboard on a hybrid tablet

Issue Description: I manage a small group of about 90 users who are crucial to our business. When one or two users from this group request changes to the user interface (UI) of their web app, we allocate development resources to meet their needs. However, ...

I need to send information from my JavaScript code to my Flask server

I'm having an issue with transferring data from JavaScript code in an HTML template to my Flask server. Specifically, I want to send geolocation coordinates (latitude and longitude) obtained through JavaScript to my Flask server, but I'm unsure o ...