简体   繁体   English

将JSON数据从一个控制器传递到其他angularjs

[英]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. 我有一个登录服务,该服务向我提供JSON对象作为具有用户详细信息的响应。 Now in this response, there's an object named permissions which gives me information about the CRUD permission allowed to the user. 现在,在此响应中,有一个名为权限的对象,该对象向我提供了有关允许用户使用的CRUD权限的信息。

It looks like this : 看起来像这样:

在此处输入图片说明

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). 我们知道我们不能在服务/工厂中通过$ scope,我不知道如何进行操作(我不想使用$ rootScope)。

This is an issue about learning how to pass data between two controllers via a factory or service. 这是有关学习如何通过工厂或服务在两个控制器之间传递数据的问题。 You don't need to pass the $scope variable because you should be binding the data to an object in the factory. 您不需要传递$ scope变量,因为您应该将数据绑定到工厂中的对象。

Here is a simple example: 这是一个简单的示例:

Controller 1 控制器1

myApp.controller('Ctrl1', ['$scope', 'myFactory', function($scope, myFactory){
    myFactory.data = someFunctionThatChangesTheData();
}]);

Controller 2 控制器2

myApp.controller('Ctrl2', ['$scope', 'myFactory', function($scope, myFactory){
    $scope.data = myFactory.data;
}]);

and finally your Factory 最后是你的工厂

myApp.factory('myFactory', function(){
    return {
        data: {
           value: 'some data'
        }
    }
});

You could then use the data from Controller two in a view: 然后,您可以在视图中使用来自控制器2的数据:

View 2 (Assigned Ctrl2) 查看2 (已分配Ctrl2)

<div>
    <h1>View 2</h1>
    <p>{{data.value}}</p>
</div>

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

});

I got around a same problem. 我遇到了同样的问题。 I used the solution which will not be likable or I would say there must be a better approach using angular service or factory to do that (I don't know about those). 我使用的解决方案不那么讨人喜欢,或者我想必须使用更好的方法使用角度服务或工厂来做到这一点(我对此一无所知)。

The solution I worked with is using sessionStorage or localStorage of HTML5. 我使用的解决方案是使用HTML5的sessionStorage或localStorage。 I know its not the angular way of doing things but it will get the job done. 我知道这不是做事的有角度的方法,但是它将完成工作。

In Controller 1 在控制器1中

sessionStorage.setItem('permissions', JSON.stringify($scope.permissionArry));

And to get the item in controller 2, 并将其放入控制器2中

$scope.permissions = JSON.parse(sessionStorage.getItem('permissions'));

I personally think that there must exist better solutions than this and you should think about learning angular factory or service to solve the problem. 我个人认为,必须有比这更好的解决方案,并且您应该考虑学习角度工厂或服务以解决问题。 But meanwhile you can use this solution for a quick way around 但是与此同时,您可以使用此解决方案快速解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angularjs中将登录数据从一个控制器传递到另一个控制器而不是使用rootscope - how to pass login data from one controller to another other than using rootscope, in angularjs 如何在angularjs中将对象数据从一个控制器传递到另一个控制器 - how to pass object data from one controller to another controller in angularjs 使用工厂将数据从一个控制器传递到AngularJS中的另一个控制器 - Using a factory to pass data from one controller to another controller in AngularJS 将 JSON 数据从一个控制器传递到另一个控制器 angularJS - passing JSON data from one controller to another controller angularJS Angular.js无法将JSON数据传递给控制器​​的指令 - Angularjs Can't pass json data to directive from controller 通过POST将JSON数据从AngularJS传递到Laravel Controller - Pass JSON data from AngularJS to Laravel Controller via POST 如何将此json数据从服务器传递到控制器? Node.js / AngularJS - How to pass this json data from server to controller? Nodejs / AngularJS AngularJS:使用同一控制器将数据从一个视图传递到另一个视图 - AngularJS : Pass data from one view to another with in same controller AngularJS通过控制器将数据从一个指令发送到另一指令 - Angularjs send data from one directive to other via controller 如何在angularjs中将数据从一个控制器发送到另一个 - How to send data from one controller to other in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM