我从该函数得到响应,但无法使用angularjs显示它

时间:2017-03-06 05:29:51

标签: javascript 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":"imran1@gmail.com",
 "password":"pass","city":"","address":"imran@gmail.com","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 个答案:

答案 0 :(得分:1)

您正以错误的方式分配数据:

注意:通过传递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
     });
    }
  });

做类似的事情:

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