2017-03-06 100 views
1

这是我的angularjs代码和我得到的控制台响应,但无法显示它来显示它..我越来越从功能响应,但无法与angularjs

{{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"); 
} 

}); 

输出

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

你在错误的方式来分配数据传回:

注:成功回调是通过传递response对象作为第一个参数调用,它含有一种叫0属性引用响应数据的。

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 
    }); 
    } 
    }); 

做类似:

{{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. 
    }); 
} 
}); 
+1

从在响应中的对象的数据属性是一个阵列,指针添加到元件实际显示在视图中的数据 – nosthertus

+0

@nosthertus是的,你是对的,我不知道,因为在我写答案的时候,OP没有提到响应数据。我现在已经更新了答案。 – Gaurav

+0

感谢它现在正在工作 –