为什么表达式databaseService[user1Constant].accountBalance
会产生以下错误?
angularJsApplicationController.js
:
app.controller("angularJsController", function ($scope, $location, $http, user1Constant, user2Constant, databaseService) {
databaseService[user1Constant+'Request'].done(function(){
console.log(JSON.stringify(databaseService));
console.log(user1Constant);
console.log(databaseService);
console.log(databaseService[user1Constant]);
console.log(databaseService[user1Constant].accountBalance);
});
});
这是我创建对象的代码:
angularJsApplication.js
:
app.service("databaseService", ["user1Constant","user2Constant", function (user1Constant, user2Constant) {
var returnObject = {};
returnObject.updateAllEntriesUser = function (user) {
//jQuery AJAX HTTP method
$.post("getEntriesFromUser.php", {
user: user
})
.done(function (response) {
response = JSON.parse(response);
returnObject[user]={};
returnObject[user].bookings = response;
var totalAmount = 0;
for (var i = 0; i < response.length; i++) {
totalAmount += parseFloat(response[i].amount_before_comma + '.' + response[i].amount_after_comma);
}
returnObject[user].accountBalance = totalAmount;
});
};
returnObject[user1Constant + 'Request'] = returnObject.updateAllEntriesUser(user1Constant);
returnObject[user1Constant + 'Request'] = returnObject.updateAllEntriesUser(user2Constant);
return returnObject;
}]);
答案 0 :(得分:0)
控制台向您显示请求完成后当前的对象状态,这与您将其写入控制台时的状态不同。您的控制器正在尝试在加载数据之前访问数据,这就是您的用户数据没有您期望的属性的原因。
您需要从服务中返回一个jQuery延迟对象,然后将一个函数传递给它的
► iMac@~:brew info openssl
openssl: stable 1.0.2h (bottled) [keg-only]
SSL/TLS cryptography library
https://openssl.org/
/usr/local/Cellar/openssl/.openssl (8B)
Poured from bottle on 2016-02-15 at 21:07:44
/usr/local/Cellar/openssl/1.0.2f (1,651 files, 11.9M)
Poured from bottle on 2016-02-15 at 21:07:44
/usr/local/Cellar/openssl/1.0.2g (1,678 files, 12.0M)
Poured from bottle on 2016-03-07 at 18:32:36
/usr/local/Cellar/openssl/1.0.2h (1,691 files, 12M)
Poured from bottle on 2016-05-07 at 21:17:01
/usr/local/Cellar/openssl/1.0.2h_1 (1,691 files, 12M)
Poured from bottle on 2016-05-12 at 23:09:46
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl.rb
函数,以便您可以等到获取数据直到它被加载。
或者你可以欺骗并使用$ scope。$ watch ...
您可以将其更改为更灵活,但要使其使用最少量的更改:
done
你的控制器:
app.service("databaseService", ["user1Constant","user2Constant", function (user1Constant, user2Constant) {
var returnObject = {};
returnObject.updateAllEntriesUser = function (user) {
//jQuery AJAX HTTP method
return $.post("getEntriesFromUser.php", {
user: user
})
.done(function (response) {
response = JSON.parse(response);
returnObject[user]={};
returnObject[user].bookings = response;
var totalAmount = 0;
for (var i = 0; i < response.length; i++) {
totalAmount += parseFloat(response[i].amount_before_comma + '.' + response[i].amount_after_comma);
}
returnObject[user].accountBalance = totalAmount;
});
}
returnObject[user1Constant + 'Request'] = returnObject.updateAllEntriesUser(user1Constant);
returnObject[user2Constant + 'Request'] = returnObject.updateAllEntriesUser(user2Constant);
console.log(returnObject); //angularJsApplication.js:101
return returnObject;
});