-1

It must be able to pass data between route controllers. Like for example have an input field in one route where its contents will be displayed in the next route. I don't want it with $rootScope.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
});
</script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>

<a href="">Click to see this data to another page (route)</a>
</div>
</body>
</html>
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Write down a service for sharing data between controllers. – Gaurav Singh May 04 '19 at 18:24
  • How can i do that? i am not getting it. can you help me ? –  May 04 '19 at 18:27
  • How does the app change routes? Is this a SPA (Single-Page Application)? Or does it do a full page re-load to change routes? As written, the question is too broad. – georgeawg May 04 '19 at 20:24
  • Possible duplicate of [Angularjs Best Practice for Data Store](https://stackoverflow.com/questions/31626360/angularjs-best-practice-for-data-store) – dota2pro May 04 '19 at 21:33

1 Answers1

1

See This

RipTutorial - Handling session storage through service using angularjs

app.factory('storageService', ['$rootScope', function($rootScope) {

    return {
        get: function(key) {
            return sessionStorage.getItem(key);
        },
        save: function(key, data) {
            sessionStorage.setItem(key, data);
        }
    };
}]);

app.controller('myCtrl',['storageService',function(storageService) {

  // Save session data to storageService
  storageService.save('key', 'value');

  // Get saved session data from storageService
  var sessionData = storageService.get('key');

});
Community
  • 1
  • 1
dota2pro
  • 7,220
  • 7
  • 44
  • 79