0

我正在尝试比较控制器函数中的两个日期,并根据控制器中的逻辑向 ng-show 和 ng-hide 返回真/假,但它没有按预期工作,下面是相同的 plnkr。有人可以帮我找出问题所在。

http://plnkr.co/edit/CWvb1uy0PeYFb0Tf34rY?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope,$filter) {
  $scope.name = 'World';
  $scope.SetDate=function(dt)
  {
    $scope.NewDt=$filter('date')(dt, 'shortDate');
     $scope.currentDate = $filter('date')(new Date(), 'shortDate');   
     $scope.parsedCurr=Date.parse($scope.currentDate);
     $scope.parsedFuture= Date.parse($scope.NewDt);

        if ( $scope.parsedCurr <$scope.parsedFuture) {
            return true;
        }  
  }

});
4

2 回答 2

1

你否认了两次,你有:

<span ng-hide="!SetDate('2015-12-31T00:00:00')">

你需要

<span ng-hide="SetDate('2015-12-31T00:00:00')">
于 2015-02-20T18:27:33.047 回答
1

如果您只需要检查两个解析日期,我会在 ng-show 中进行检查以简化代码。

<span ng-show="parsedCurr < parsedFuture">
      You can see me!
</span>
<span ng-hide="parsedCurr < parsedFuture">
      You can't see me :(
</span>

或者,如果您不想使用该功能,只需稍微更改逻辑并使用 ng-show 或 ng-hide。像这样:

$scope.SetDate=function(dt)
  {
    $scope.NewDt=$filter('date')(dt, 'shortDate');
     $scope.currentDate = $filter('date')(new Date(), 'shortDate');   
     $scope.parsedCurr=Date.parse($scope.currentDate);
     $scope.parsedFuture= Date.parse($scope.NewDt);

        if ( $scope.parsedCurr < $scope.parsedFuture) {
            return true;
        }  
        else
        {
            return false;
        }
  }

你的标记是这样的:

<span ng-show="SetDate('2015-12-31T00:00:00')">
          SHOW ME
</span>
<br>
<span ng-hide="SetDate('2015-12-31T00:00:00')">
          HIDE ME
</span>
于 2015-02-20T18:34:16.743 回答