0

$scope.restaurants variable is emty. how to pass data to $scope.restaurants?

FoodSearchControllers.controller('homeCtrl', ['$scope', '$http', function($scope, $http) {
//restoranai
$http.post('http://appdev.milasevicius.com/server/index.php', {
    "query": "SELECT * FROM n01_restaurant"
}).success(function(data, status) {
    $scope.restaurants = data;
}).error(function(data, status) {
    $scope.data = data || "Request failed";
    $scope.status = status;
});

console.log($scope.restaurants);
}]);

Also, controller is called twice. Why is that?

1
  • It is empty because it the call is asynchronous. But will be filled upon successful response (assuming your back-end is configured properly). Commented Dec 2, 2013 at 20:43

1 Answer 1

1

Your console.log is called at a point where the $http call might not have returned yet. The post is an async call so if you move your console.log in your success function, you should see it. In short, $scope.restaurants is mostly likely being populated but you're trying to check it too early.

Sign up to request clarification or add additional context in comments.

Comments

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.