2

It's my angularjs code and i'm getting response in console But Can't display it..

{{user.email}}


 var app = angular.module('myApp', []);
 app.controller('apppCtrl', function($scope,$http) {
  $scope.displayProfile=function(){
    $http.get("../api/user_data.php")
    .then(function(data){
        $scope.user=data
    })
     // alert("Angularjs call function on page load");
}

});

Output

{"data":[{"id":"10","name":"Imran","last_name":"","email":"[email protected]",
 "password":"pass","city":"","address":"[email protected]","gender":"","dob":"","img":""}],
"status":200,
"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],
"jsonpCallbackParam":"callback",
    "url":"../api/user_data.php",
    "headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"OK"}

1 Answer 1

1

You are assigning the data back in the wrong manner:

Note: The success callback is called by passing response object as the first argument and it contains a property called data that references the response data.

var app = angular.module('myApp', []);
app.controller('apppCtrl', function($scope,$http) {


$scope.displayProfile=function(){
    $http.get("../api/user_data.php")
    .then(function(data){
        $scope.user=data; // <-- This is the problem
     });
    }
  });

Do something like:

{{user[0].email}}

var app = angular.module('myApp', []);
app.controller('apppCtrl', function($scope,$http) {


$scope.displayProfile=function(){
    $http.get("../api/user_data.php")
    .then(function(response){
        $scope.user = response.data; // <-- Do like this.
    });
 }
});
Sign up to request clarification or add additional context in comments.

4 Comments

the data property from the object in the response is an array, add a pointer to the element to actually display the data in the view
@nosthertus Yes you are right and I didn't know that because at the time when I was writing the answer OP hasn't mentioned the response data. I have now updated the answer.
Thanks It's Working Now
ImranIqbal, Glad to help! and @nosthertus Thanks for pointing out.

Your Answer

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