Search code examples
angularjsangularjs-serviceangularjs-factory

Pass JSON data from one controller to other angularjs


I've a login service which gives me JSON object as a response having user details. Now in this response, there's an object named permissions which gives me information about the CRUD permission allowed to the user.

It looks like this :

enter image description here

Now I've different pages, each page having a table. What I want is to check the permission of the logged in user and according to that show/hide the element to create/read/update/delete the record from the table.

What I am currently doing is saving the object onto an array :

            $scope.permissionArry = [];
            for (var i = 0; i < data.permissions.length; i++) {
                $scope.permissionArry.push({
                    moduleId: data.permissions[i].module_id,
                    createModule: data.permissions[i].create_module,
                    readModule: data.permissions[i].read_module,
                    updateModule: data.permissions[i].update_module,
                    deleteModule: data.permissions[i].delete_module
                });
             }

and then trying to pass this array to other controller.

As we know we cannot pass $scope in service/factory I don't know how to proceed (I don't want to use $rootScope).


Solution

  • It very simple you need to use service to achieve this:

    example can be found link

        MyApp.app.service("xxxSvc", function () {
    
    var _xxx = {};
    
    return {
        getXxx: function () {
            return _xxx;
        },
        setXxx: function (value) {
            _xxx = value;
        }
    };
    
    });