这是引发问题的行:
submitInfo(groups, $scope.Tab, other, drug, "Saved", false)
.then(function () {
})
奇怪的是,可以识别SubmitInfo(这是一种注入服务)(我在那里设置了一个断点)。仅当组为空数组时,才会引发此错误。我不明白为什么会这样或如何防止它。我希望即使组为空也要执行该功能。
这是submitInfo:
summariesApp.factory('submitInfo', ['$http', '$q', 'validate', 'getInfo', '$rootScope', 'toaster', function ($http, $q, validate, getInfo, $rootScope, toaster) {
return function (groups, Tab, other, drug, state, auto) {
var deferred = $q.defer();
if (groups.length == 0) {
return;
}
if (state != "Saved") {
if (!validate(groups, Tab, other, drug, "submit")) {
return;
}
}
var deferral = $q.defer();
$http({
url: "ThirtySecond.aspx/SubmitSummary",
method: "POST",
data: { type: Tab, groups: groups, summaryState: state, other: other, drug: drug },
headers: { 'Content-Type': 'application/json; charset=utf-8' }
})
.success (
function () {
getInfo(Tab, other, drug)
.then(function (data) {
var newGroups = data;
for (i = 0; i < groups.length; i++) {
var group = groups[i];
var newGroup = $rootScope._.find(newGroups, 'Grouptype', group.Grouptype);
newGroup.show = group.show;
newGroup.Checked = group.Checked;
}
switch (state) {
case "Saved":
if (auto) {
successMessage = "Auto Saved";
}
else {
successMessage = "Saved";
}
break;
case "SubmittedForReview":
successMessage = "Submitted for review";
break;
case "NoSummaryWarranted":
successMessage = "Summary updated as 'No Summary Warranted'";
break;
case "FinalizedButNotRolledOut":
successMessage = "Summary updated as 'Finalized but not ready to roll out'";
break;
case "Finalized":
successMessage = "Summary finalized";
break;
default:
successMessage = "Draft Entered";
}
toaster.pop('success', "Updated", successMessage);
angular.extend(groups, newGroups);
deferral.resolve(true);
})
}
)
.error(
function () {
toaster.pop('error', "Error!", "Something went wrong. Please contact the administrator.");
deferral.reject(true);
}
)
return deferral.promise;
}
}]);
最佳答案
由于使用了submitInfo
语句,undefined
返回了return;
if (groups.length == 0)
{
return;
}
将所有
return;
替换为return deferred.promise;
(因为deferral
尚不可用),您的代码应该可以正常工作。如果您无法更改
submitInfo
,也可以在使用||
调用函数时建立后备(基于undefined
评估为false
的事实):(submitInfo(groups, $scope.Tab, other, drug, "Saved", false) || /* get your promise here */)
.then(function () {
})
(我不确定,要实现承诺的代码是
$scope.Tab.defer().promise
还是summariesApp.factory
弄乱了参数?)关于javascript - angular-当参数之一为空数组时出现奇怪的错误“无法读取未定义的属性”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29899218/