AngularJS - 比较资源和对象(AngularJS - Comparing resources and objects)
在Angular中,一些方法调用返回Objects ,一些方法返回Resources ,这使得它们难以比较。 我有一个用户模型和一个权限模型。 用户模型定义为:
myApp.service('User', function($resource) { var User = $resource('users/:userId.json'); return User; });
并定义了权限模型:
myApp.service('Permission', function($resource) { var Permission = $resource('permissions'); return Permission; });
当我检索用户时,返回的json具有用户的权限:
{"id":1,"username":"Admin","group_id":"1","permissions":[{"id":1,"name":"Create Users"},"id":2,"name":"Delete Users"}]}
这意味着当我检索用户时,我可以使用.permissions访问他/她的权限。例如
User.get({userId: 1}).permissions
我想将其与所有权限列表进行比较,并获取用户尚未拥有的权限列表:
$scope.user = User.get({userId: 1}); Permission.query().$promise.then(function(all){ $scope.user.$promise.then(function(u){ $scope.unselected_permissions = _.difference(all, u.permissions); }); });
问题是,
all
都是一个资源数组,而u.permissions
是一个对象数组。 是否有一种简单的方法可以让u.permissions
返回资源列表? 是否有更简单的事情可以做而不是等待承诺在这里解决?In Angular, some method calls return Objects, and some return Resources, which make them difficult to compare. I have a user model and a permission model. The user model is defined:
myApp.service('User', function($resource) { var User = $resource('users/:userId.json'); return User; });
And the permission model is defined:
myApp.service('Permission', function($resource) { var Permission = $resource('permissions'); return Permission; });
When I retrieve a user, the json returned has the user's permissions:
{"id":1,"username":"Admin","group_id":"1","permissions":[{"id":1,"name":"Create Users"},"id":2,"name":"Delete Users"}]}
This means when I retrieve a user, I can access his/her permissions with .permissions For example
User.get({userId: 1}).permissions
I want to compare this with the list of all permissions, and get a list of the permissions that the user does not already have:
$scope.user = User.get({userId: 1}); Permission.query().$promise.then(function(all){ $scope.user.$promise.then(function(u){ $scope.unselected_permissions = _.difference(all, u.permissions); }); });
The problem is,
all
is an array of Resources, andu.permissions
is an array of Objects. Is there a simple way to getu.permissions
to return a list of resources? Is there something simpler I can do instead of waiting for the promises to resolve here?
原文:https://stackoverflow.com/questions/24789080
最满意答案
返回一个承诺而不是隐藏它
$scope.userLogin = function (info) { return $http.post('/authenticate_user', info); }
用法
userLogin({name: '', password: ''}).then(function(success) { console.log(success) })
Return a promise instead of hiding it
$scope.userLogin = function (info) { return $http.post('/authenticate_user', info); }
usage
userLogin({name: '', password: ''}).then(function(success) { console.log(success) })
相关问答
更多-
这很可能是因为CORS 。 除非服务器设置指定您的域的Access-Control-Allow-Origin标头,否则无法将Ajax请求提交到您所在的域以外的域。 为了验证这一点,浏览器首先发送preflight OPTIONS请求,似乎您的服务器响应该请求的错误。 这不适用于提交html表单,因为它不是AJAX请求。 It's most likely because of CORS. Ajax requests cannot be submitted to domains other than the o ...
-
实际上,当我们使用$ http的POST从AngularJs发送数据时,它会将带有content-type =“application / json”的数据发送到服务器。 而Django并不了解这种格式。 所以你无法获得发送的数据。 解决方案是使用以下配置更改内容类型标头: app.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.headers.post['Content-Type' ...
-
AngularJS $ http.post json请求以表达待处理状态(AngularJS $http.post json request to expressjs in pending status)[2021-01-27]
如果发生错误,您只会发送回复,所以如果成功,也会发送一些回复 function(err, todo) { console.log(todo); if (!err) return response.send(todo); // or send whatever or respnose.end() console.log('bbb'); response.send(err); }); You are only sending a response ... -
节点js上的$ http请求 - 如何成功返回值$ http.post($http request on node js - how to return a value in success $http.post)[2023-05-13]
返回一个承诺而不是隐藏它 $scope.userLogin = function (info) { return $http.post('/authenticate_user', info); } 用法 userLogin({name: '', password: ''}).then(function(success) { console.log(success) }) Return a promise instead of hiding it $scope.userLogin = func ... -
事实证明,身体解析器设置是一个错误,我只设置应用程序使用urlencoded,而不是json。 It turned out to be an error with the body-parser setup, I'd only set the app to use urlencoded, not json.
-
请求标头字段在预检响应中,Access-Control-Allow-Headers不允许使用Content-Type Access-Control-Allow-Headers不支持通配符。 您正在使用*但必须明确指定允许的标头。 我尝试更新Access-Control-Allow-Headers标头,如下所示。 Access-Control-Allow-Headers是响应头。 您必须在服务器上设置它,而不是在客户端上。 您的客户端JavaScript无法授予其读取其他人数据的权限。 这将使得获得许可毫无意 ...
-
问题是如何将数据传输到服务器。 这是因为jQuery和Angular以不同方式序列化数据。 默认情况下,jQuery使用Content-Type: x-www-form-urlencoded传输数据Content-Type: x-www-form-urlencoded和熟悉的foo=bar&baz=moe序列化。 然而,AngularJS使用Content-Type: application/json传输数据Content-Type: application/json和{ "foo": "bar", "ba ...
-
问题出在服务器端。 你需要为选项实现hanlder。 例: app.options("*",function(req,res,next){ res.header("Access-Control-Allow-Origin", req.get("Origin")||"*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); //other headers he ...
-
XMLHttpRequest无法加载https://mipage.com/examplephp 。 请求标头字段在预检响应中,Access-Control-Allow-Headers 不允许使用Content-Type 。 如果我将$ http.post更改为$ http.get,请求使用JSON的服务器响应,所以..我认为CORS没有问题。 我还在离子项目中加了代理。 说明 : 当您通过$ http.get()传递一个HTTP GET请求传递params对象时,它们被追加到查询字符串中,并且由于该请求没有 ...
-
你在寻找的是: $scope.$watch("message", function(value){ console.log($scope.message); }); 这是我制作的示例plnkr示例 What you are looking for is this: $scope.$watch("message", function(value){ console.log($scope.message); }); Here is a sample plnkr example I've made ...