繁体   English   中英

在angularjs中说早上和晚上

[英]say morning evening and afternoon in angularjs

我想创建一条指令,在晚上和晚上进行巡视。 如果我会用Java脚本编写代码,这将是逻辑

var time = 00;


/* hour is before noon */
if ( time < 12 ) 
{
    document.write("Good Morning!");
}
else  /* Hour is from noon to 5pm (actually to 5:59 pm) */
if ( time >= 12 && time <= 17 )
{
    document.write("Good Afternoon!");
}
else  /* the hour is after 5pm, so it is between 6pm and midnight */
if ( time > 17 && time <= 24 )
{
    document.write("Good Evening!");
}
else  /* the hour is not between 0 and 24, so something is wrong */
{
    document.write("I'm not sure what time it is!");
}

如何在Angular中使用指令来实现这一点

我的角度应用程序dom是

<p> 12:00:01 </p>

我希望它呈现为<p>afternoon </p>

我更喜欢过滤器而不是指令,它很容易实现,如下所示。

过滤:

app.filter('greet', function() {
  return function(input) {
    if (input < 12) {
      return 'Good Morning';
    } else if (input >= 12 && input <= 17) {
      return 'Good Afternoon';
    } else if (input > 17 && input <= 24) {
      return 'Good Evening';
    } else {
      return "I'm not sure what time it is!";
    }
  };
});

HTML: <span>{{value | greet}}</span> <span>{{value | greet}}</span>

示例演示: http : //plnkr.co/edit/5zs1zrxgeL7OT7ssT7Ak?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM