I am very new at angularJS. I have been to show all customers from mysql using. So, I wrote as in cotroller and service as:
app.controller('CustomersController', function ($scope, customersService, $http) {
init();
function init() {
$scope.customers = customersService.getCustomers();
}
});
app.service('customersService', function ($http) {
this.getCustomers = function () {
return customers;
};
// my issue is here
$http.get("app/server/read.php")
.success(function(data){
var customers = data;
});
});
In php I have written:
$result = mysqli_query($con,"SELECT * FROM customers");
$return_arr = array();
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$rowArr = array(
'id' => $row['id'],
'firstName' => $row['firstname'],
'lastName' => $row['lastname'],
'address' => $row['address'],
'city' => $row['city']
);
$return_arr[] = $rowArr;
}
echo json_encode($return_arr);
php returns the json array as:
[{"id":"36","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"37","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"38","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"39","firstName":"","lastName":"","address":"","city":""},{"id":"40","firstName":"asd","lastName":"asd","address":"","city":"asd"}]
I do not understand how to put this json object array into the variable customers because when I write directly this array into the variable customers it works. i.e.
var customers = [{"id":"36","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"37","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"38","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"39","firstName":"","lastName":"","address":"","city":""},{"id":"40","firstName":"asd","lastName":"asd","address":"","city":"asd"}]
But I can't put it dynamically that after success of get method result.