TB.M
TB.M

Reputation: 363

How to call another service variable data in any service/controller using angular js

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

So, This timeStamp value I want in another service call in different page.How I can call this timeStamp and display there?

I am not much friendly to coding..Please help.

Upvotes: 1

Views: 88

Answers (1)

Aman
Aman

Reputation: 838

for data sharing across the controller in angular JS you can use localstorge, sessionstorage or $rootScope

way to share data through localstorage :

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

way to share data through sessionStorage:

 sessionStorage.setItem(key, value);

 // to get the data 

 sessionStorage.getItem(key, value);

way to share data through 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)
      });

Upvotes: 2

Related Questions