2017-03-06 36 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

感謝它現在正在工作 –