我想创建一个返回 morning evening 和 afternoon 的指令。如果我用 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 中实现这一点
我的 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>
关于javascript - 在angularjs中说早上晚上和下午,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078171/