2

I have the following code in which I fetch data from the JSON file , I Store data in $scope.users variable, But I want to fetch only username value how can I do this?

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


    ]
}
4
  • 3
    It should be response.data.data[0]["username"] as the inner data is an array Commented Apr 11, 2017 at 12:21
  • response.data.data[0]["username"] Commented Apr 11, 2017 at 12:22
  • thankyou so much @PankajParkar :) Commented Apr 11, 2017 at 12:29
  • thanks @Edison :') Commented Apr 11, 2017 at 12:29

1 Answer 1

2

you can get value for that JSON value as

response.data.data[0]["username"]

If you want to get all the usernames from the array, then you can do something like this:

var arr = response.data.map(function(a, b){
   return a.username;
});

arr will contain all the username details

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.