Javascript Angular中的Return语句不适用于对象
非常基本的东西。。。似乎它应该工作,但没有 这里的两个控制台日志都显示了正确的信息Javascript Angular中的Return语句不适用于对象,javascript,angularjs,object,client,frontend,Javascript,Angularjs,Object,Client,Frontend,非常基本的东西。。。似乎它应该工作,但没有 这里的两个控制台日志都显示了正确的信息 $scope.getCurrentAttachment = function() { angular.forEach(attachments, function(attachment) { console.log(attachment); if(attachment.active) { console.log(attachment); return attachment
$scope.getCurrentAttachment = function() {
angular.forEach(attachments, function(attachment) {
console.log(attachment);
if(attachment.active) {
console.log(attachment);
return attachment;
}
});
};
但是稍后在调用它的文件中会变成未定义
$scope.save = function() {
console.log( $scope.getCurrentAttachment());
var data = [$scope.labels.selected, $scope.getCurrentAttachment()];
console.log(data);
$uibModalInstance.close(data);
};
任何帮助都是非常感激的。我不知道为什么这样不行
$scope.getCurrentAttachment = function() {
angular.forEach(attachments, function(attachment) {
console.log(attachment);
if(attachment.active) {
console.log(attachment);
return attachment;
}
});
};
这里的return
子句用于angular.forEach函数,而不是getCurrentAttachment
您可以使用数组ptotype中的.filter
函数执行以下操作
$scope.getCurrentAttachment = function() {
var filteredArray = []
filteredArray = attachments.filter(function(attachment) {
console.log(attachment);
return attachment.active;
});
return filteredArray ;
};
什么是“附件”?它没有作为参数传递到$scope.getCurrentAttachment中。范围变量,全局变量,…?什么应该起作用?什么是未定义的
?$scope.getCurrentAttachment
函数还是附件
变量?使用$scope.getCurrentAttachment()的返回未定义。另外,附件是一个填充的对象数组,将成功显示在logPerfect中,谢谢。最后我修改代码的方式有点不同。我只是设置了一个范围变量,而不是使用函数的返回。不管怎样,你的信息非常有用。非常感谢!当你的回答让我满意时,我会接受的