1
我有以下代碼,我從JSON文件中獲取數據,我將數據存儲在$scope.users
變量中,但我只想獲取用戶名值,我該如何做到這一點?JavaScript:如何從JSON數據獲取特定值
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 == "[email protected]")
{
if(password == "admin")
{
$state.go('app.home');
}
}
};
}]);
User.json
{
"result": "success",
"data": [
{ "id": 1, "username":"[email protected]", "password":"admin"}
]
}
應該是'response.data.data [0] [ 「用戶名」]'的'內部數據'是一個數組 –
'response.data.data [0] [「username」]' – Edison
謝謝你這麼多@PankajParkar :) –