2016-09-17 2 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')

참고하십시오 : 당신이 잘못된 날짜의 오류가 발생하는 경우가 임의의 장소에서 사용하지 때문에 또한 $scope.jdate= new Date();을 제거 할 수 있습니다 jdate: new Date('04/23/2013')

처럼 월과 일 플립해야 할 수도 있습니다.

+0

감사합니다 ... – HAraTest

+1

또는 [ISO 8601] (https://en.wikipedia.org/wiki/ISO_8601) 날짜 형식'날짜 ('2013-04-23')를 사용하십시오. ' – Lipis