简体   繁体   English

在ng-repeat中带有ng-if的AngularJS函数

[英]AngularJS Function with ng-if inside ng-repeat

I have some times that are calculated dynamically and repeat in an ng-repeat like so: 我有一些时间是动态计算的,并且在ng-repeat中重复,如下所示:

<div class="col-sm-4" ng-repeat="time in scheduling.openTimes">
   <label class="label label-default label-time" ng-class="{'active' : scheduling.militaryTime == time.military}" ng-click="scheduling.setTime(time.military)" ng-if="!scheduling.booked(time.military)">
        {{time.display}}
</label>
</div>

And the function scheduling.booked() gets called on each label. 并且在每个标签上调用了scheduling.booked()函数。 It should either return true if the time is "booked" or false if not. 如果时间已“预定”,则返回true;否则返回false。

I want the time to display if the time is NOT BOOKED . 如果时间还NOT BOOKED确定,我想显示时间。 My function looks like so: 我的函数如下所示:

        scheduling.booked = function(time)
    {
        ref.child('appointments').once('value', function(snapshot){
            snapshot.forEach(function(childSnapshot){
                var data = childSnapshot.val();
                var sysDate = new Date(scheduling.date);
                var appDate = new Date(data.date);
                var appTime = data.time.military;

                if(appDate.getDay() == sysDate.getDay())
                {
                    if(appTime == time)
                    {
                        return true
                    }
                    else
                    {
                        return false
                    }
                }
                else
                {
                    return false
                }
            })
        })

    }

It consoles out everything like it should but the label is not hiding? 它可以控制一切,但标签没有隐藏吗? The console shows that is should be. 控制台显示应该是。

update 更新

through some research and lots of deleting, I've came up with this. 通过一些研究和大量删除,我想到了这一点。 It works and does what I want as long as you only have one appointment. 只要您只有一次约会,它就能正常工作并满足我的要求。 If you have more than one, it duplicates the time slot. 如果您有多个,它将​​复制该时隙。 How would you make it so it checked if the time was in the array already and skip it if it is? 您将如何使它检查时间是否已经在数组中,如果已跳过该时间呢?

scheduling.booked = function(time, timeDis) {

        ref.child('appointments').once('value', function(snapshot) {
            snapshot.forEach(function(childSnapshot) {
                var data = childSnapshot.val();
                var sysDate = new Date(scheduling.date).getDate();
                var appDate = new Date(data.date).getDate();
                var appTime = data.time.military;
                if(sysDate == appDate)
                {
                    if(appTime == time)
                    {
                        $scope.openTimes.push({
                            'military' : time,
                            'display'  : timeDis,
                            'disabled' : true
                        })
                    }
                    else
                    {
                        $scope.openTimes.push({
                            'military' : time,
                            'display'  : timeDis,
                            'disabled' : false
                        })
                    }
                }
                else
                {
                    $scope.openTimes.push({
                        'military' : time,
                        'display'  : timeDis,
                        'disabled' : false
                    })
                }

            })
        });

        $timeout(function(){
            scheduling.openTimes = $scope.openTimes;
            scheduling.timeLoading = false;
        }, 1300)

    }

I have another function calling this one now, I've ditched the ng-if. 我现在有另一个函数调用此函数,我放弃了ng-if。

Your function scheduling.booked does not have a return . 您的功能scheduling.booked没有return So it will always return undefined . 因此它将始终返回undefined So when angular interprets ng-if="!scheduling.booked(time.military)" will be ng-if="true" ( !undefined equals true ). 因此,当角度解释ng-if="!scheduling.booked(time.military)"将为ng-if="true"!undefined等于true )。 This explains why all records are shown. 这解释了为什么显示所有记录。

I think the following code should work. 我认为以下代码应该工作。

scheduling.booked = function(time) {
  var booked = false;

  ref.child('appointments').once('value', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var data = childSnapshot.val();
      var sysDate = new Date(scheduling.date);
      var appDate = new Date(data.date);
      var appTime = data.time.military;

      if (appDate.getDay() == sysDate.getDay() && appTime == time) {
        booked = true;
      }
    })
  });

  return booked; // <-- make sure the function has a return value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM