在AngularJS中加载当前Week的数据

时间:2017-03-13 06:16:53

标签: javascript angularjs

我想在AngularJS中检索当前周日期和日期名称(如:星期一,星期二)。周应从星期一开始。如下图所示:

enter image description here

点击下周按钮,下周的日期和日期名称将加载到前端。

注意:周数据应从互联网上加载。它不应该加载系统的日期。

我试图谷歌,没有得到这种解决方案。

先谢谢。

1 个答案:

答案 0 :(得分:1)

使用 moment.js ,您可以执行以下操作以获取当前周和下一周:

<强> controller.js

function TodoCtrl($scope) {
var currentDate = moment();

 var fnWeekDays = function(dt) {

    var currentDate = dt;
    var weekStart = currentDate.clone().startOf('week');
    var weekEnd = currentDate.clone().endOf('week');

    var days = [];
    for (i = 1; i <= 7; i++) {

        days.push(moment(weekStart).add(i, 'days').format("D (ddd)"));

    };
    return days;
}

$scope.weekDays = fnWeekDays(currentDate);
$scope.nextWeek = function(dt) {
    $scope.weekDays = fnWeekDays(moment(dt, "D (ddd)").add(1, 'days'));
};
$scope.previousWeek = function(dt) {
    $scope.weekDays = fnWeekDays(moment(dt, "D (ddd)").subtract(1, 'days'));
};


}

<强> HTML

<div ng-app>
<div ng-controller="TodoCtrl">
<a ng-click="previousWeek(weekDays[0])">Previous</a>
<a ng-click="nextWeek(weekDays[6])">Next</a>
<div ng-repeat="day in weekDays">
  {{day}}
</div>

   

这是Fiddle