javascript-重复调用Ng-Options表达式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-重复调用Ng-Options表达式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3702字,纯文字阅读大概需要6分钟。
内容图文
我的< select>有多个问题元素的角度,并试图了解正在发生的事情.我的第一步是了解为什么我反复输入的用于调试的多个console.log()消息会在控制台中重复出现,而不是像我期望的那样出现一次,而是出现无数次,就像如果是无限循环的一部分.从ng-options调用的函数应该如何表现?如果是这样,我不明白为什么,如果不是,那么我想修复我的循环.
我的html:<选择ng-options =“ theorderdate作为getOtherOrderDates(Bread.text)中的订购日期的订购日期” ng-model =” randomDateRomantic.randomDateRomantic“ ng-change =” soRomantic(Bread.text)“>< /选择> 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循环,它也无法解释它始终记录在控制台上的事实,因为最终循环将无法满足,并且这不会花费只要已经提到过.
请指教,谢谢.如果您需要更多信息,我们很乐意提供.
解决方法:
将在每个digest cycle中调用getOtherOrderDates,以便angular知道是否更新select中的选项.这很可能是您多次看到此方法的原因.
如果您担心此循环对性能的影响,可以在控制器内部预先构建选项,将其存储在$scope中,如下所示:
$scope.options = $scope.getOtherOrderDates($scope.Bread.text);
只要$scope.Bread.text更改,然后在模板中使用$scope.options.
内容总结
以上是互联网集市为您收集整理的javascript-重复调用Ng-Options表达式全部内容,希望文章能够帮你解决javascript-重复调用Ng-Options表达式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。