ng-if使用==运算符进行日期比较

时间:2016-04-25 05:20:25

标签: angularjs

ng-if="{{(Date | date:"dd/mm/yyyy") == (question.DateTimeEnd | date:"dd/mm/yyyy")}}"

我的表达式为$scope.Date = new Date()

我收到解析器错误。我想知道是否可以使用==本身的mustache expression运算符来实现这一目标。

2 个答案:

答案 0 :(得分:5)

试试这样。你不需要{-}}在ng-if和#34;"以日期格式。

  ng-if="(Date | date:'dd/MM/yyyy') == (question.DateTimeEnd | date:'dd/MM/yyyy')"



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

app.controller('MainCtrl', function($scope) {
  $scope.Date = '20160313T00:00:00';
  
  $scope.DateTimeEnd = '20160313T00:00:00';
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <div ng-app="app" ng-controller="MainCtrl">
   
        <p  ng-if="(Date | date:'dd/MM/yyyy') == (DateTimeEnd | date:'dd/MM/yyyy')" ng-bind="DateTimeEnd | date:'dd/MM/yyyy'"></p>
   
 </div>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

试试这个: -

.controller('myController', function ($scope) {

   // Convert Date into long format for comparision by using .getTime()

   $scope.tempDate = new Date();
   $scope.date = $scope.tempDate.getTime();

   $scope.DateTime = $scope.question.DateTimeEnd.getTime(); 
})

然后使用ng-if

ng-if="{{date == DateTime}}"

或者您也可以用日期格式打印结果

ng-if="{{date == DateTime | date : "dd/MM/yyyy"}}"