Javascript AngularJS:内部函数不能指定外部函数的变量值
我正在开发离子混合应用程序。我使用$http从服务器获取值。接下来,我将进行cordova sqlite查询,在cordova查询中,我希望将来自服务器的$http调用的结果和cordova查询的结果插入我的sqlite数据库。但是,我无法在cordova查询中获取$http返回值。以下是我的代码:Javascript AngularJS:内部函数不能指定外部函数的变量值,javascript,angularjs,sqlite,cordova,Javascript,Angularjs,Sqlite,Cordova,我正在开发离子混合应用程序。我使用$http从服务器获取值。接下来,我将进行cordova sqlite查询,在cordova查询中,我希望将来自服务器的$http调用的结果和cordova查询的结果插入我的sqlite数据库。但是,我无法在cordova查询中获取$http返回值。以下是我的代码: $http({ method: "post", url: "http://localhost/php2/get_channel.php", data: { u
$http({
method: "post",
url: "http://localhost/php2/get_channel.php",
data: {
user_name: usernameID
},
headers: { 'Content-Type': 'application/json' }
}).success(function(response) {
for(i=0; i<response.length; i++){
var channelNameQuery = "SELECT * FROM chat_friend WHERE channel_id=?"
var channelNamePromise = $cordovaSQLite.execute(db, channelNameQuery, [response[i].ChannelName]).then(function (result){
var ChannelQuery = "REPLACE INTO chat_channel (last_text, usernameID, chat_channel, chat_channel_name) VALUES (?,?,?,?)";
$cordovaSQLite.execute(db, ChannelQuery, [response[i].LastText, usernameID, response[i].ChannelName, result.rows.item(0).UserName]);
})
}
}).error(function(response) {
console.log(response);
console.log("failed");
});
$http({
方法:“张贴”,
url:“http://localhost/php2/get_channel.php",
数据:{
用户名:usernameID
},
标题:{'Content-Type':'application/json'}
}).成功(功能(响应){
对于(i=0;i您接收到的数据映射到response.data
。请尝试使用angular.forEach()
在您的数据中循环。请记住response
主要是一个对象,因此您无法在此处获取response.length
。请查看
向我们显示响应json数据
$http({
method: "post",
url: "http://localhost/php2/get_channel.php",
data: {
user_name: usernameID
},
headers: { 'Content-Type': 'application/json' }
}).success(function(response) {
angular.forEach(response.data, function (data) {
var channelNameQuery = "SELECT * FROM chat_friend WHERE channel_id=?"
var channelNamePromise = $cordovaSQLite.execute(db, channelNameQuery, [data.ChannelName]).then(function (result){
var ChannelQuery = "REPLACE INTO chat_channel (last_text, usernameID, chat_channel, chat_channel_name) VALUES (?,?,?,?)";
$cordovaSQLite.execute(db, ChannelQuery, [data.LastText, usernameID, data.ChannelName, result.rows.item(0).UserName]);
})
});
}).error(function(response) {
console.log(response);
console.log("failed");
});