2016-09-17 43 views
0

我开始使用自定义过滤器,日期过滤器和ng-repeat编写示例。它基本上是日期属性作为我的模型的一部分。AngularJS中的DateField

<!DOC TYPE html> 
    <html> 
    <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"> 
    </script> 
    <script> 
    var customfilter= angular.module("filterapp",[]) 
    customfilter.controller("filterctrl",function($scope){ 
     $scope.jdate= new Date(); 
     $scope.tabledata=[ 
          {numbers:1, 
          firstName:"Eve", 
          lastName:"Jackson", 
          salary:45000, 
          jdate:'23/04/2013' 
          }, 

          {numbers:2, 
          firstName:"John", 
          lastName:"Doe", 
          salary:55000, 
          jdate:'22/02/2010'}]; 

    }); 
    customfilter.filter("taxcalc",function(){ 
     return function(salary){ 
        if(salary > 50000) 
      { 
        tax= salary * (20/100); 
      } 
      else if(salary > 40000) 
      { 

       tax = salary * (10/100); 
      } 

      else{ 

       tax= salary * (5/100); 
      } 
      return tax; 
     } 
    }); 
    </script> 
    </head> 
    <body ng-app="filterapp"> 
    <div ng-controller="filterctrl"> 
    <table> 
    <thead> 
    <th>numbers</th> 
    <th>firstname</th> 
    <th>lastname</th> 
    <th>salary</th> 
    <th>joiningdate</th> 
    <th>tax</th> 
    </thead> 
    <tbody> 
    <tr ng-repeat="arraydata in tabledata"> 
    <td>{{arraydata.numbers}}</td> 
    <td>{{arraydata.firstName}}</td> 
    <td>{{arraydata.lastName}}</td> 
    <td>{{arraydata.salary}}</td> 
    <td>{{arraydata.jdate| date: 'shortDate'}}</td> 
    <td>{{arraydata.salary | taxcalc}}</td> 
    </tr> 
    </tbody> 
    </table> 
    </div> 
    </body> 
    </html> 

回答

2

你的日期应该是日期对象而不是字符串。 而不是jdate: '23/04/2013'尝试jdate: new Date('23/04/2013')

注:如果您收到无效的日期错误,你可能需要翻转日和月像jdate: new Date('04/23/2013')

您还可以删除$scope.jdate= new Date();因为它不是任何地方使用。

+0

谢谢...它的工作 – HAraTest

+1

或者绝对可以使用[ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)日期格式Date('2013-04-23') ' – Lipis