0

I am trying to break up this json response into multiple arrays that I can access on the front end. I've got a json response such as:

 {
"clients": [
{
  "id": "2",
  "f_name": "test",
  "l_name": "test",
  "email": "[email protected]",
  "company_name": "asd",
  "street": "asd",
  "city": "asd",
  "state": "asd",
  "zip": "54913",
  "identity_id": "6",
  "admin_id": "1",
  "created_at": "2015-01-22 22:55:38",
  "updated_at": "2015-02-04 04:03:13"
},
{
  "id": "3",
  "f_name": "dsf",
  "l_name": "df",
  "email": "[email protected]",
  "company_name": "asdf",
  "street": "sdf",
  "city": "asdf",
  "state": "asdf",
  "zip": "asdf",
  "identity_id": "6",
  "admin_id": "1",
  "created_at": "2015-01-23 17:49:51",
  "updated_at": "2015-01-23 17:49:51"
}
   ],
  "identity": {
"id": "6",
"name": "Test Company",
"street": "13 Street",
"city": "An",
"state": "State",
"zip": "54913",
"admin_id": "1",
"created_at": "2015-01-17 15:34:12",
"updated_at": "2015-02-04 03:43:40",
"image": "https:\/\/s3_whatever",
"phone": "",
"email": "",
"paypal": "whatever_whatever.com"
  }
}

I would like to have 'clients' put into $scope.clients and identity put into $scope.identity

Here is my code:

 var app = angular.module("myApp", []);

 app.controller("TestController", function ($scope, $http) {


$http.get('http://localhost:8000/clients/json').success(function(clients) {
    $scope.clients = clients.clients;
    $scope.identity = clients.identity; 
});


 });

Here is the front end code:

    <div ng-app="myApp">

 <div ng-controller="TestController">
<ul>

 <li ng-repeat="client in clients">
 {{ client.email }}
 </li>

</ul>
 </div>

 <h1>
 {{ identity }}
 </h1>

 </div>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js">     </script>
<script src='/js/app.js'></script>
5
  • Is it not working? your $http code block looks correct Commented Feb 6, 2015 at 23:01
  • It is not. I also attempted to use $scope.clients = clients.clients (and $scope.identity = clients.identity) which also does not return the identity. The clients array seems to work OK with this front end code: * added to the top * Commented Feb 6, 2015 at 23:03
  • 1
    Are you sure the json is in the format that you posted. I copied and pated it and it's working fine: Plunkr Commented Feb 6, 2015 at 23:08
  • I am copy/pasting from the actual response. I updated my code above to include the the entire app.js file Commented Feb 6, 2015 at 23:10
  • Also updated the entire front end code... maybe i am doing something wrong there? Commented Feb 6, 2015 at 23:14

1 Answer 1

1

Your closing out your controller and making identity out of scope

<div ng-app="myApp">  
    <div ng-controller="TestController">
        <ul>
            <li ng-repeat="client in clients">
                {{ client.email }}
            </li>
        </ul>
    </div><!--END OF TEST CONTROLLER AND $Scope FOR THAT CONTROLLER -->   
    <h1>
 {{ identity }}
 </h1>  
</div>

to fix it just move your h1 into the controller div

<div ng-app="myApp"> 
    <div ng-controller="TestController">
        <ul> 
            <li ng-repeat="client in clients">
                {{ client.email }}
            </li>
        </ul>
       <h1>{{ identity }}</h1>
    </div><!-- end of TestController -->
</div><!-- end of myApp -->
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.