flyingkandi
flyingkandi

Reputation: 3

Passing a value from one controller to another one

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.

Upvotes: 0

Views: 4669

Answers (1)

kTT
kTT

Reputation: 1350

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();
    };
});

Upvotes: 3

Related Questions