角度和NG选项和JSON [英] angular and ng-options and json
本文介绍了角度和NG选项和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样一个JSON:
{
0:{登录:USER1,许可证:{X:4},开放:真正},
1:{登录:用户2,许可证:{X:6,XX:9,XXX:7},开放:真正}
}
这
<选择NG模型=userToAddNG选项=user.login作为user.login用户在listUsers>< /选择>
不工作,但我不知道为什么。
下面是相关的控制器code:
$ scope.getUsers =功能(){
$ scope.listUsers = {};
变种indexLogin = 0;
变种indexLicense = 0;
$ http.get('LISTUSER')。成功(功能(数据){
_.each(数据,功能(许可证,用户登陆){
$ scope.listUsers [indexLogin] = {};
$ scope.listUsers [indexLogin]的.login =用户登陆;
$ scope.listUsers [indexLogin] .licenses = {};
_.each(许可证,功能(许可){
如果(在$ scope.listUsers [indexLogin] .licenses license.feature.name){
$ scope.listUsers [indexLogin] .licenses [license.feature.name] = $ scope.listUsers [indexLogin] .licenses [license.feature.name] + 1;
}其他{
$ scope.listUsers [indexLogin] .licenses [license.feature.name] = 1;
}
indexLicense ++;
})
$ scope.listUsers [indexLogin]。开= FALSE;
indexLogin ++;
})
});
};
解决方案
您 listUsers
是一个对象,而不是一个数组。所以NG选项应该相应改变:
NG选项=在listUsers user.login作为user.login为(_,用户)
另外,也可以使它成为一个合适的阵列要换 listUsers
格式。而是采用递增 indexLogin
,刚建立在每一个步骤一个新的对象,然后推这个对象为使用 listUsers
阵列线 _地图
来prepare对象的数组正确。
$ scope.listUsers = [];
$ http.get('LISTUSER')。成功(功能(数据){
$ scope.listUsers = _.map(数据,功能(许可证,用户登陆){
VAR用户= {
登录:用户登陆,
打开:假的,
许可:{}
};
_.each(许可证,功能(许可){
VAR featureName = license.feature.name;
user.licences [featureName] =(user.licences [featureName] || 0)+ 1;
});
返回用户;
})
});
I have a json like :
{
"0":{"login":"user1","licenses":{"x":4},"open":true},
"1":{"login":"user2","licenses":{"x":6,"xx":9,"xxx":7},"open":true}
}
And this
<select ng-model="userToAdd" ng-options="user.login as user.login for user in listUsers"></select>
Doesn't work, but I don't know why.
Here's the relevant controller code:
$scope.getUsers = function() {
$scope.listUsers = {};
var indexLogin = 0;
var indexLicense = 0;
$http.get('listUser').success(function(data) {
_.each(data, function(licenses, userLogin){
$scope.listUsers[indexLogin] = {};
$scope.listUsers[indexLogin].login = userLogin;
$scope.listUsers[indexLogin].licenses = {};
_.each(licenses, function(license){
if(license.feature.name in $scope.listUsers[indexLogin].licenses){
$scope.listUsers[indexLogin].licenses[license.feature.name] = $scope.listUsers[indexLogin].licenses[license.feature.name] + 1;
} else {
$scope.listUsers[indexLogin].licenses[license.feature.name] = 1;
}
indexLicense++;
})
$scope.listUsers[indexLogin].open = false;
indexLogin++;
})
});
};
解决方案
Your listUsers
is an object, not an array. So the ng-options should be changed accordingly:
ng-options="user.login as user.login for (_, user) in listUsers"
Alternatively, you might want to change listUsers
format so it becomes a proper array. Instead of using incrementing indexLogin
, just create a new object at each step, then push this object into use listUsers
array_.map
to prepare a proper array of objects:
$scope.listUsers = [];
$http.get('listUser').success(function(data) {
$scope.listUsers = _.map(data, function(licenses, userLogin){
var user = {
login: userLogin,
open: false,
licences: {}
};
_.each(licenses, function(license){
var featureName = license.feature.name;
user.licences[featureName] = (user.licences[featureName] || 0) + 1;
});
return user;
})
});
这篇关于角度和NG选项和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文