AngularJS中的DateField

时间:2016-09-17 14:45:10

标签: javascript html angularjs

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

 <!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>

1 个答案:

答案 0 :(得分:2)

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

注意:如果您收到无效日期错误,则可能需要像jdate: new Date('04/23/2013')

一样翻转日期和月份

您也可以删除$scope.jdate= new Date();,因为它在任何地方都没有使用过。