【问题标题】:How to just show 4 digit year in Angular?如何在 Angular 中只显示 4 位数的年份?
【发布时间】:2014-09-25 04:00:33
【问题描述】:

我找到了Angular Date documentation here,并认为过滤器 yyyy 就足够了,但它仍然附加到实际日期。

我的 HTML:

<p class="footer_copy">&copy; {{ footer.date | date : yyyy }} AT&amp;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

【问题讨论】:

    标签: javascript angularjs date


    【解决方案1】:

    你需要用来传递格式为字符串

    var app = angular.module('my-app', [], function() {})
    
    app.directive('appFooter', function () {
        return {
            restrict: 'E',
            template: '<p class="footer_copy">&copy; {{ footer.date | date:"yyyy" }} AT&amp;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">&copy; {{ footer.date | date:footer.dateFormat }} AT&amp;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>

    【讨论】:

    • 我可以将该控制器合并到我的指令中,对吗?不愿意浪费整个控制器只是为了吐出一个日期。它们将如何组合成我现在拥有的东西?
    • @LeonGaban 是的.. 你可以
    • 啊,我的 yyyy 周围缺少引号,'yyyy' 谢谢! 4 分钟
    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多