本文介绍了使用Angular显示今天的生日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从团队列表中显示今天的生日".我有一个包含其生日(例如1999年12月16日)和姓名的数据.我正在使用此显示即将出生的婴儿.但我想显示今天生日"块.请帮助

I want to display Today birthday from a team list. I'm having a data containing their Birthday (eg 16/12/1999) and Names. I'm displaying the upcoming birth using this. But i want to display Today Birthday block. please Help

<script>
function birthdayCtrl($scope){

$scope.friends=[
  {name:'Pete','birthday':"12/25/1983"},
  {name:'Sarah','birthday':"02/14/1985"},
  {name:'James','birthday':"01/15"},
  {name:'Lilly','birthday':"04/01/1975"},
  {name:'John','birthday':"08/07/1955"},

  ]

  $scope.friends.forEach(function(data){
      var day = data.birthday.split("/")
      var currentYear = new Date().getFullYear();
      var birthdayDate = new Date(currentYear, day[0] - 1, day[1])
      var now = new Date().valueOf();
      if (birthdayDate.valueOf() < now){ 
          birthdayDate.setFullYear(currentYear+1)
      }
      data.fromNow = birthdayDate.valueOf() - now;
  })
}

</script>
<body ng-controller="birthdayCtrl">
    <h1>Upcoming Birthdays</h1>
    <table>
    <th>No.</th>
    <th>Name</th>
    <th>Birthday</th>
    <tr ng-repeat="friend in friends| orderBy:'fromNow' ">
    <td>{{$index}}</td>
    <td>{{friend.name}}</td>
    <td>{{friend.birthday}}</td>

    </tr>
    </table>
    <style>
     table td,table th{
    border: thin solid #eaeaea;
    width:150px;
    padding:3px 5px;
    </style>
  </body>

推荐答案

几种方法可以做到这一点.一种方法是使用 ng-if ="todayBirthday?onlyToday(friend):true" : http://plnkr.co/edit/kMGIlNcESjg4kYE3oHhY?p=preview

A few ways to do this. One way is to use ng-if="todayBirthday ? onlyToday(friend) : true": http://plnkr.co/edit/kMGIlNcESjg4kYE3oHhY?p=preview

$scope.onlyToday = function(friend) {
  var today = new Date();

  if(friend.birthDate.getDate() === today.getDate() && friend.birthDate.getMonth() === today.getMonth())
    return true;
  else
    return false;
}

这篇关于使用Angular显示今天的生日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 09:09