我的<select>
元素在角度上遇到多个问题,并试图了解发生了什么。我的第一步是了解为什么我反复输入的用于调试的多个console.log()
消息为什么会在控制台中重复出现,而不是像我期望的那样出现一次,而是出现无数次,就像部分无限循环这是从ng-options
调用的函数应该如何表现的吗?如果是这样,我不明白为什么,如果不是,那么我想修复我的循环。
我的html:<select ng-options="theorderdate as theorderdate for theorderdate in getOtherOrderDates(Bread.text)" ng-model="randomDateRomantic.randomDateRomantic" ng-change="soRomantic(Bread.text)"></select>
console.log()
消息从下面的getOtherOrderDates()
函数中出现(包括我的评论):
$scope.getOtherOrderDates = function(loaf) {
var istheLoafdaily = false;
var theorderdates = [];
for (var i = 0; i < $scope.theBreadsList.length; i++) {
for (var z = 0; z < $scope.theBreadsList[i].breads.length; z++) {
if ($scope.theBreadsList[i].breads[z].text == loaf && i > 0) //not a daily loaf, goes beyond "Daily Breads"
{
console.log(theorderdates);
theorderdates = theorderdates.concat($scope.theBreadsList[i].breads[z].orderDates); //concat the matched bread's order dates
console.log(theorderdates, $scope.theBreadsList[i].breads[z].orderDates);
theorderdates = _.sortBy(theorderdates, function(m) {
return m.getTime()
});
for (var y = 0; y < theorderdates.length; y++) {
theorderdates[y] = theorderdates[y].toLocaleDateString();
}
theorderdates = _.uniq(theorderdates);
if (theorderdates.length > 0) {
console.log("Something is wrong here"); //problem
$scope.randomDateRomantic.randomDateRomantic = theorderdates[0];
}
console.log(theorderdates);
return theorderdates;
} else if ($scope.theBreadsList[i].breads[z].text == loaf && i == 0) { //a daily loaf, i == 0
console.log("The bread matched is daily", loaf); //***
istheLoafdaily = true;
console.log(theorderdates); //***
theorderdates = theorderdates.concat($scope.theBreadsList[i].breads[z].orderDates); // concat the matched bread's order dates
console.log(theorderdates, $scope.theBreadsList[i].breads[z].orderDates); //***
break; // escape the for loop, should it be two breaks?????? yes...
} else if (istheLoafdaily && i > 0 && $scope.theBreadsList[i].breads[z].orderDates.length > 0) { //not sure what scenario this matches, hence wtf
theorderdates = theorderdates.concat($scope.theBreadsList[i].breads[z].orderDates);
console.log("wtf");
}
}
}
//end of outermost for loop
//not sure what this is doing because this functionality is repeated up there^ (for non-daily breads)
theorderdates = _.sortBy(theorderdates, function(m) {
return m.getTime()
});
for (var y = 0; y < theorderdates.length; y++) {
theorderdates[y] = theorderdates[y].toLocaleDateString();
}
theorderdates = _.uniq(theorderdates);
if (theorderdates.length > 0) {
$scope.randomDateRomantic.randomDateRomantic = theorderdates[0];
console.log("Something is wrong here (daily)"); //problem
}
return theorderdates;
//not sure what this is doing because this functionality is repeated up there^ (for non-daily breads)
//if change to Locale date string then not unique, but if don't change then not a date to sort!!!!!!! >:(
},
我几乎无数次获得所有控制台消息,而没有执行任何操作,例如触发
ng-change
函数。例如,我只是将日常面包添加到购物车中,然后控制台中填充了以下已加注星标的消息。我的
theBreadsList
不太长,所以有这样的事情在反复发生。即使如您在代码中所见,即使我两次退出for循环,它也无法解释它始终记录在控制台上的事实,因为最终循环将无法满足,并且这不会花费只要提到过。请指教,谢谢。如果您需要更多信息,我们很乐意提供。
最佳答案
getOtherOrderDates
将在每个digest cycle中被调用,以便angular知道是否更新option
中的select
。这很可能是您多次看到此方法的原因。
如果您担心此循环对性能的影响,可以在controller
内部预先构建选项,将其存储在$scope
中,如下所示:
$scope.options = $scope.getOtherOrderDates($scope.Bread.text);
每当
$scope.Bread.text
更改时,然后在模板内使用$scope.options
。