The main problem is that you're using jQuery's $.getJSON()
method, which Angular doesn't know anything about. Angular is great at tracking when asynchronous events occur and updating the view when they complete, but you have to use Angular's services to take advantage of this.
Angular gives us the $http
service for making AJAX calls. Since $http
is tied into Angular's digest cycle (read up on this if you've never heard of it, it's critical to understanding how Angular works), Angular will update the view after $http
returns data and its callbacks are run.
Rewrite your controller like this and it should work:
app.controller('VELControl', function($scope, $http) {
$http.get("../json/dict.php?p="+$_GET['p']).then(function(response){
$scope.velData = response.data;
});
});
As an aside, your alert()
working with the old method was probably a fluke based on how fast your server returned the data. As I understand it, alert()
runs asynchronously anyway, so when you called alert($scope.velData)
, this was probably pushed onto the end of the browser's execution queue, and before it was reached the AJAX call returned and set the value you needed. If your server had been slower, you probably would have alerted undefined
and then received your data. If you have control over the server (which it looks like you do), this would probably be a fun experiment to try: put in a sleep(10)
or some similar wait to force the server to take a long time; try different values and see what happens with that alert()
statement.