重复调用Ng选项表达式 [英] Ng-Options Expression Repeatedly Called

查看:78
本文介绍了重复调用Ng选项表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的角色中的< select> 元素存在多个问题,我正在尝试了解发生了什么。我的第一步是理解为什么多次 console.log()消息我已经在控制台中重复显示,如同在,而不是像一次出现的消息一样我希望,它们看起来无数次,就好像是无限循环的一部分。这是一个从 ng-options 调用的函数应该如何表现的?如果是这样,我不明白为什么,如果没有,那么我想修复我的循环。

I am having multiple issues with my <select> element in angular and am trying to understand what is going on. My first step is to understand why the multiple console.log() messages I have put in for debugging repeatedly appear in the console, as in, instead of the message appearing once like I would expect, they appear an infinite number of times, as if part of an infinite loop. Is this how a function called from an ng-options is supposed to behave? If so, I don't understand why, if not, then I would like to fix my loop.

我的html:< select ng -options =theorderdate作为getOtherOrderDates(Bread.text)中orderdate的orderdateng-model =randomDateRomantic.randomDateRomanticng-change =soRomantic(Bread.text)>< / select>

getOtherOrderDates()<中出现 console.log()消息/ code>函数,位于下面(包括我的评论):

The console.log() messages appear from the getOtherOrderDates() function, which is below (with my comments included):

$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 功能。我只是在我的购物车中添加每日面包,然后控制台充满了以下消息,我已经加入了我的代码。

I am getting almost all console messages an infinite number of times, without doing anything such as firing the ng-change function. I just add a daily bread to my cart for instance, and then the console gets filled with the following messages, that I have starred in my code.

我的 theBreadsList 不是很长,所以有一些事情发生了它反复出现这种情况。即使我在代码中看到两次for循环,也不会解释它一直记录到控制台的事实,因为最终循环不会满足如上所述,这不会花很长时间。

My theBreadsList is not very long, so there is something going on that it is going repeatedly like this. Even if I broke out of the for loop twice as you will see in my code, it wouldn't explain the fact that it logs to the console all the time, because eventually the loop would not be satisfied, and this wouldn't take to long as has been mentioned.

请告知,谢谢。如果您需要更多信息,我很乐意提供。

Please advise, thank you. If you need more information, I am happy to provide.

推荐答案

将在每个 getOtherOrderDates /docs.angularjs.org/guide/scope#scope-life-cycler el =nofollow>摘要周期,以便angular 知道是否在中更新选项选择。这很可能是你看到这种方法被多次调用的原因。

The getOtherOrderDates will be called in each digest cycle so that angular knows whether to update options in select. That's most likely the reason you're seeing this method being called many times.

如果您担心此循环对性能的影响,您可以在控制器中预先构建选项存储它在 $ scope 中如下:

If you're worried about performance impact of this loop you can build the options upfront inside your controller store it in $scope like so:

$scope.options = $scope.getOtherOrderDates($scope.Bread.text);

然后在模板中使用 $ scope.options

这篇关于重复调用Ng选项表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆