Minearena Mine
Minearena Mine

Reputation: 23

How can I create a scope function the return json in Angular?

I have a function like this:

 $scope.GetDays = function() {
         $http.post("core/ajax/LoadDays.php").success(function(data){
                 return data[0].days;
    }); 

};

And in LoadDays.php I have this json:

 [{"days":"1"}]

If I do console log It will return correct: 1. But, the problema is When I call it on my HTML code. I recive a looping erros : $rootScope:infdig

How can I do this?

Sorry for my english

Upvotes: 2

Views: 82

Answers (1)

wvdz
wvdz

Reputation: 16651

Read up on the concept of Ajax (asynchronous javascript), because that's what you're doing here.

You should do something like this:

$scope.GetDays = function() {
         $http.post("core/ajax/LoadDays.php").success(function(data){
                 $scope.days= data[0].days;
    }); 

};

And then use {{days}} in your html. Days will be filled with data shortly after you call GetDays(), depending on how fast the server request is handled.

Upvotes: 2

Related Questions