0

I am trying to pass a value from one controller to another one. Example: page 1(controller A) has 5 lists of values. I clicked the 3rd value and so I am directed to page 2(controller B). Here I need to get data based on the value I clicked in the page 1(controller A).

Example:

page 1 list of students 
id   name    
1    mark
2    Dev
3    Nancy 

page 2 details of student
    name=mark
    subject  grade
    English  A
    Spanish  A-

I am using spring-hibernate to get data from a database.

Gargaroz
  • 313
  • 9
  • 28
flyingkandi
  • 3
  • 3
  • 9

1 Answers1

3

You can create a service to store this object

angular.module('app').factory('StoreService', function () {
        var storedObject;
        return {
            set: function (o) {
                this.storedObject = o;
            },
            get: function () {
                return this.storedObject;
            }
        };
});

and then inject it in both controllers.

angular.module('app').controller('FirstController', function ($scope, StoreService) {
    $scope.setValue = function (value) {
        StoreService.set(value);
    };
});

angular.module('app').controller('SecondController', function ($scope, StoreService) {
    $scope.getValue = function () {
        $scope.value = StoreService.get();
    };
});
kTT
  • 1,340
  • 11
  • 19
  • can you please tell me how to inject it to the controllers. – flyingkandi Jun 15 '15 at 04:56
  • @flyingkandi I updated my answer with that information – kTT Jun 15 '15 at 06:23
  • thank you @kTT. the value is passed form 'firstController' to storeObject.set(o="abc"). but after that i am redirected to page2.jsp. at that moment the value in o(o="abc") is no more there in the o. can you please look into this – flyingkandi Jun 15 '15 at 17:54
  • @flyingkandi How you make your redirect? Check this fiddle https://jsfiddle.net/ctawL4t3/, I set and get value between two controllers. – kTT Jun 15 '15 at 19:18