我有以下代码,我从JSON文件中获取数据,我将数据存储在$scope.users
变量中,但我只想获取用户名值,我该怎么办呢?
LoginCtrl.js
'use strict';
angular.module('User').controller('LoginCtrl', ['$scope','$state', "SettingService","UserService", function($scope,$state, SettingService,UserService) {
$scope.users = [];
UserService.getLoginInfo().then(function(response){
$scope.users = response.data.data["username"];
}, function(error){
})
var vm = this;
vm.doLogin = function(){
var username = document.forms["loginForm"]["email"].value;
var password = document.forms["loginForm"]["password"].value;
if(username == "admin@gmail.com" )
{
if(password == "admin")
{
$state.go('app.home');
}
}
};
}]);
User.json
{
"result": "success",
"data": [
{ "id": 1, "username":"admin@gmail.com", "password":"admin"}
]
}
答案 0 :(得分:2)
您可以将该JSON值的值作为
response.data.data[0]["username"]
如果你想从数组中获取所有用户名,那么你可以这样做:
var arr = response.data.map(function(a, b){
return a.username;
});
arr
将包含所有用户名详细信息