L
L
levchak09102015-06-13 21:45:59
Angular
levchak0910, 2015-06-13 21:45:59

Why is the variable not updated immediately after the http request?

Hello. I'm new to angular so I'm asking for help explaining some of the nuances.
Question: why are the variables not updated immediately after the request?
So we have a controller, a factory and a file handler
Factory

app.factory('ld',function($http)
{
  text="11111";
  $http.post('php5.php').success(function(d){text=d;});
  return {al:function(){console.log(text)}}
});

Controller
app.controller('cont',function($http,ld)
{
  ld.al();
  setTimeout(function(){ld.al();},1000);
});

file handler
header('Content-Type: application/json');
$text="22222";
echo json_encode($text);

And here is the result in the console:
11111
22222

Is it possible to make variables in angular update immediately?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Kononenko, 2015-06-13
@premas

God, format the code properly. And give adequate names to controllers and services...

app.controller('cont', ['$http', '$timeout', 'ld', function($http, $timeout, ld) {
  ld.al();
  $timeout(function(){ld.al();},1000);
}]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question