Javascript 如果在jquery中重复,则重复将不起作用
函数stationMenu($scope){ } 我在哪里做的Javascript 如果在jquery中重复,则重复将不起作用,javascript,jquery,Javascript,Jquery,函数stationMenu($scope){ } 我在哪里做的 function stationMenu($scope){ $scope.phones = [ {"name": "Nexus S", "snippet": "Fast just got faster with Nexus S."}, {"name": "Motorola XOOM™ with Wi-Fi", "snippet": "The N
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."}
];
}
它可以工作……我怎样才能使它在ajax中工作呢。。关于这一点,现在有很多问题。
首先,我假设您在$scope.phones中输入的值是从ajax请求返回的,并且不是硬编码的,否则硬编码值就没有意义了
默认情况下,jquery中的ajax请求是异步的。
因此,您需要对返回的数据执行的所有操作都需要在
ajax请求的success
事件中执行
所以在你的样本中
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);
}
使用when,then构造处理从服务器返回的数据。@autolycus:无法理解问题标题。
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);
}
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."}
];
});