Reputation: 28284
function stationMenu($scope){
$.ajax({
url: "/users/station_names_ajax",
type: "POST",
success: function(data){
$scope.phones = [
{"name": "Nexus S",
"snippet": "Fast just got faster with Nexus S."},
{"name": "Motorola XOOM™ with Wi-Fi",
"snippet": "The Next, Next Generation tablet."},
{"name": "MOTOROLA XOOM™",
"snippet": "The Next, Next Generation tablet."}
];
//console.log(Stations);
}
});
// $scope.phones = Stations;
// console.log(Stations);
}
where as if I do this
function stationMenu($scope){
$scope.phones = [
{"name": "Nexus S",
"snippet": "Fast just got faster with Nexus S."},
{"name": "Motorola XOOM™ with Wi-Fi",
"snippet": "The Next, Next Generation tablet."},
{"name": "MOTOROLA XOOM™",
"snippet": "The Next, Next Generation tablet."}
];
}
it works....how can I make it work within ajax
Upvotes: 2
Views: 66
Reputation: 4764
function callService(){
return $.ajax({
url: "/users/station_names_ajax",
type: "POST",
success: function(data){
//console.log(Stations);
}
});
}
var $scope= {};
$.when(callService())
.then(function(data){
$scope.phones = [
{"name": "Nexus S",
"snippet": "Fast just got faster with Nexus S."},
{"name": "Motorola XOOM™ with Wi-Fi",
"snippet": "The Next, Next Generation tablet."},
{"name": "MOTOROLA XOOM™",
"snippet": "The Next, Next Generation tablet."}
];
});
Use the when, then construct to work with the data returned from the server.
Upvotes: 1
Reputation: 12705
here you go again.. there are a lot of questions about this now.
first of all im assuming that the values you are putting in the $scope.phones is being returned from ajax request and isnt hardcoded otherwise it would make no meaning to hard code the values
the ajax request in jquery is async by default.
so everything you need to do with the data returned needs to be done inside the success
event of the ajax request
so in your sample
function stationMenu($scope){
$.ajax({
url: "/users/station_names_ajax",
type: "POST",
success: function(data){
$scope.phones = [
{"name": "Nexus S",
"snippet": "Fast just got faster with Nexus S."},
{"name": "Motorola XOOM™ with Wi-Fi",
"snippet": "The Next, Next Generation tablet."},
{"name": "MOTOROLA XOOM™",
"snippet": "The Next, Next Generation tablet."}
];
//console.log(Stations);
//make use of anything returned and and $scope.phones here
}
});
//these lines wont work here
// $scope.phones = Stations;
// console.log(Stations);
}
Upvotes: 0