我开始使用自定义过滤器,日期过滤器和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>

最佳答案

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

注意:如果您收到无效的日期错误,则可能必须像jdate: new Date('04/23/2013')那样翻转日期和月份

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

09-25 17:25