0

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.

1 Answer 1

1

Return the $http promise from your service and then in the .success callback set your $scope property.

app.controller('CustomersController', function ($scope, customersService, $http) {

    init();

function init() {
    customersService.getCustomers().success(function(data){
        $scope.customers = data;
    });
}

});

app.service('customersService', function ($http) {
    this.getCustomers = function () {
        return $http.get("app/server/read.php");
    };
});
Sign up to request clarification or add additional context in comments.

4 Comments

if I use in this way $http.get("app/server/read.php") .success(function(data){ var customers = data; }); });, I found that there is an error that is, customers is not define. Please could you tell me how to define the customers because if I follow your way, other operation is affected.
var customers defines customers but you don't want to user var, you want to add it to the scope like I do in my answer.
Yes I understand, but is it possible to pull data from mysql and put into a variable then add it to scope?
Yes var something = data; $scope.customers = something;

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.