【发布时间】:2018-05-21 15:00:24
【问题描述】:
我正在尝试将此 JavaScript 函数转换为 AngularJS,但由于我是 AngularJS 的新手,所以很少出错。
var array = [5, 5, 7, 9, 9, 9];
var max = array[0],
total = 0;
array.forEach((a) => {
if (a == max) {
total += max;
} else if (a > max) {
max = total = a;
}
});
console.log("total:", total);
var array[] 现在来自 GET 数据,我在 AngularJS 代码中执行此操作。
$scope.List = getAllList;
$scope.deptime =function (){
$scope.array = $scope.List.Time;
console.log($scope.array);
$scope.max = $scope.array[0], $scope.total = 0;
angular.array.forEach((a)=>{
if(a==$scope.max){
$scope.total+=$scope.max;
}
else if(a>$scope.max){
$scope.max = $scope.total = a;
}
});
console.log("total:"+$scope.total);
};
$scope.deptime();
所以我面临这个错误:
TypeError: 无法读取未定义的属性“0”
我哪里错了?
编辑 :- 我从这部分的服务中得到回复 :- $scope.List = getAllList;
【问题讨论】:
-
用
$scope.array替换angular.array -
@Saeed.Ataee 遇到同样的错误。
-
记录
$scope.List.Time;并插入样本 -
显示更多代码,特别是获取
$scope.List.Time的部分。很有可能这是How do I return the response from an asynchronous call?的副本 -
错误信息表示
$scope.List.Time不存在。如果您想让我们知道它为什么不存在,您必须出示minimal reproducible example。否则你必须自己调试代码。
标签: javascript arrays angularjs