早上傍晚和下午在angularjs说

时间:2014-06-06 09:12:36

标签: javascript 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!");
}

如何在角度使用指令

中实现此目的

我的角度应用程序dom是

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

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

1 个答案:

答案 0 :(得分:2)

我更喜欢使用过滤器而不是指令,它的实现非常简单,如下所示。

过滤

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>

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