Angularjs 如何使用'moment.js从'Month number'获取'Month Name'`

Angularjs 如何使用'moment.js从'Month number'获取'Month Name'`,angularjs,momentjs,Angularjs,Momentjs,我正在angularjs应用程序中使用moment.js进行日期转换。我想将月号打印为月名。我已经试过了 <p>{{item.ReviewMonth | date : 'MMMM'}}</p> {{item.ReviewMonth|date:'MMMM'} 其中,项目。以数字格式查看月份 ex-1{5 |日期:'MMMM'}其中5表示“可能” 但是它的印刷品是一月而不是五月 ex-2{4 |日期:'MMMM'}其中4表示“四月” 但是它的印刷品是一月而不是四月 如何从

我正在angularjs应用程序中使用
moment.js
进行日期转换。我想将
月号
打印为
月名
。我已经试过了

<p>{{item.ReviewMonth | date : 'MMMM'}}</p>
{{item.ReviewMonth|date:'MMMM'}

其中,
项目。以数字格式查看月份

ex-1<代码>{5 |日期:'MMMM'}

其中
5
表示“可能” 但是它的印刷品是一月而不是五月

ex-2<代码>{4 |日期:'MMMM'}

其中
4
表示“四月” 但是它的印刷品是一月而不是四月


如何从月号中获取正确的月名?

这是因为当
date
过滤器尝试将
5
转换为
date
对象时,
5
将被识别为毫秒,并将转换为
1970-01-01T00:00:00.005Z
。然后
date
过滤器将返回
January


解决方案:

moment('5', 'M').format('MMMM');

角度模块(“应用程序”,[]) .controller(“myCtrl”,函数($scope){ $scope.testDate=5; $scope.testDate2=新日期(5); });

{{testDate}date:'MMMM'}}
{{testDate2}}
您可以使用简单的javascript从月份编号中获取月份名称:

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var month_number = 1;
alert("The current month is " + monthNames[parseInt(month_number)+1]);
或者通过使用moment js,您可以得到如下结果:

var formattedMonth = moment('09', 'MM').format('MMMM'); // September

moment(
    '09',           // Desired month
    'MM'            // Tells MomentJs the number is a reference to month
).format('MMMM')    // Formats month as name

请为月份名称制作简单指令

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

<div ng-app="myApp" ng-controller="myCtrl">

<any ng-repeat="x in item">
    <my-dir myindex="x.ReviewMonth"></my-dir>
</any>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) 
{
    $scope.item = 
    [
        {
            ReviewMonth:"9"
        },
        {
            ReviewMonth:"2"
        },
        {
            ReviewMonth:"3"
        },
        {
            ReviewMonth:"4"
        },
        {
            ReviewMonth:"5"
        },
        {
            ReviewMonth:"6"
        },
        {
            ReviewMonth:"7"
        },
        {
            ReviewMonth:"7"
        }
    ]
});

app.directive('myDir', function () 
{
  return {
    restrict: 'E',
    scope: {
      myindex: '='
    },
    template:'<div>{{myindex}}</div>',
    link: function(scope, element, attrs)
    {
        var months = ["jan","feb","mar","apr","may","june","july","aug","sep","oct","nov","dec"];
        scope.myindex = months[scope.myindex];
    }
  };
})
</script>

</body>
</html>

var-app=angular.module('myApp',[]);
应用程序控制器('myCtrl',函数($scope)
{
$scope.item=
[
{
每月回顾:“9”
},
{
每月回顾:“2”
},
{
每月回顾:“3”
},
{
每月回顾:“4”
},
{
每月回顾:“5”
},
{
每月回顾:“6”
},
{
每月回顾:“7”
},
{
每月回顾:“7”
}
]
});
应用程序指令('myDir',函数()
{
返回{
限制:'E',
范围:{
我的索引:'='
},
模板:“{myindex}}”,
链接:函数(范围、元素、属性)
{
风险值月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
scope.myindex=月份[scope.myindex];
}
};
})

如果您想在角度视图中使用momentjs方法,您可以使用

在您的情况下,您可以过滤以指定您的输入应该被解释为月号,然后您可以使用来选择如何显示解析的日期

这里是一个工作示例:

angular.module('MyApp',['angularmonent']))
.controller('AppCtrl',函数($scope){
$scope.item={};
$scope.item.ReviewMonth=5;
});

{{item.ReviewMonth | amParse:'M'| amDateFormat:'MMMM'}}


当您尝试使用ng repeat时出现问题,对吗?是。我正在使用“ng repeat”。请检查这是否有帮助。@alka已发布答案,请检查
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

<div ng-app="myApp" ng-controller="myCtrl">

<any ng-repeat="x in item">
    <my-dir myindex="x.ReviewMonth"></my-dir>
</any>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) 
{
    $scope.item = 
    [
        {
            ReviewMonth:"9"
        },
        {
            ReviewMonth:"2"
        },
        {
            ReviewMonth:"3"
        },
        {
            ReviewMonth:"4"
        },
        {
            ReviewMonth:"5"
        },
        {
            ReviewMonth:"6"
        },
        {
            ReviewMonth:"7"
        },
        {
            ReviewMonth:"7"
        }
    ]
});

app.directive('myDir', function () 
{
  return {
    restrict: 'E',
    scope: {
      myindex: '='
    },
    template:'<div>{{myindex}}</div>',
    link: function(scope, element, attrs)
    {
        var months = ["jan","feb","mar","apr","may","june","july","aug","sep","oct","nov","dec"];
        scope.myindex = months[scope.myindex];
    }
  };
})
</script>

</body>
</html>