我找到了 Angular Date documentation here ,并认为过滤器 yyyy 就足够了,但它仍然在实际的一天附加。
我的 HTML:
<p class="footer_copy">© {{ footer.date | date : yyyy }} AT&T</p>
我的页脚 Angular 指令,具有创建日期的功能:
// Directive for Footer
app.directive('appFooter', function () {
return {
restrict: 'E',
templateUrl: 'templates/app-footer.html',
controller: function(){
this.date = Date.now();
},
controllerAs:'footer'
};
});
但是现在它吐出 © 2014 年 9 月 24 日 AT&T
我希望它只创建 © 2014 AT&T
最佳答案
您需要使用将格式作为字符串传递
var app = angular.module('my-app', [], function() {})
app.directive('appFooter', function () {
return {
restrict: 'E',
template: '<p class="footer_copy">© {{ footer.date | date:"yyyy" }} AT&T</p>',
controller: function(){
this.date = Date.now();
},
controllerAs:'footer'
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
<app-footer></app-footer>
</div>
var app = angular.module('my-app', [], function() {})
app.directive('appFooter', function() {
return {
restrict: 'E',
template: '<p class="footer_copy">© {{ footer.date | date:footer.dateFormat }} AT&T</p>',
controller: function() {
this.date = Date.now();
this.dateFormat = 'yyyy'
},
controllerAs: 'footer'
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
<app-footer></app-footer>
</div>
关于javascript - 如何在 Angular 中显示 4 位数年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030272/