angularjs减去值并删除最后四位数

时间:2014-09-20 11:23:02

标签: angularjs

在我的应用程序中显示基于出生日期的用户年龄

控制器

  $scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd');
  $scope.dob = 19840623 # converted user age into yyyyMMdd format

视图

  {{currentIDate - dob}} -- 300297  

如何使用过滤器删除最后4个摘要以使用户年龄?

这里是代码:

// Code goes here

function simpleController($scope, $filter) {
  $scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd');
  $scope.dob = 19840623
}
<!DOCTYPE html>
<html ng-app=''>
  <head>
    <script data-require="angular.js@1.2.22" data-semver="1.2.22" src="https://code.angularjs.org/1.2.22/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Display Age</h1>
    <div ng-controller='simpleController'>
      Today: {{currentIDate}} <br>
      DOB: {{dob}} <br>
      Sub: {{currentIDate - dob}} <br>
      Age: {{currentIDate - dob | limitTo:2}} <br>
    </div>
  </body>

</html>

1 个答案:

答案 0 :(得分:1)

使用它会起作用:

function simpleController($scope, $filter) {
  $scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd');
  $scope.dob = 19840623
}
<!DOCTYPE html>
<html ng-app=''>
  <head>
    <script data-require="angular.js@1.2.22" data-semver="1.2.22" src="https://code.angularjs.org/1.2.22/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Display Age</h1>
    <div ng-controller='simpleController'>
      Today: {{currentIDate}} <br>
      DOB: {{dob}} <br>
      Sub: {{currentIDate - dob}} <br>
      Age: {{((currentIDate - dob)/10000) | number:0 }} <br>
    </div>
  </body>

</html>