How about this: "Express different times of the day using AngularJS -

In attempting to develop a directive that can determine whether it is morning, afternoon, or evening using JavaScript logic, the following code would need to be implemented:

var time = 00;

/* Check if the hour is before noon */
if (time < 12) {
    document.write("Good Morning!");
} else if (time >= 12 && time <= 17) { /* Hour falls between noon and 5pm */
    document.write("Good Afternoon!");
} else if (time > 17 && time <= 24) { /* Hour is after 5pm but before midnight */
    document.write("Good Evening!");
} else { /* Hour does not fall within the range of 0-24 */
    document.write("I'm not sure what time it is!");
}

Now, the challenge lies in achieving this functionality within an Angular environment using directives.

The structure of my Angular application's DOM is as follows:

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

I want this timestamp to render as <p>afternoon </p>.

Answer №1

A preference for using a filter over a directive is shared, and the implementation can be quite straightforward as shown below.

Filter:

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>

Example Demonstration: http://plnkr.co/edit/5zs1zrxgeL7OT7ssT7Ak?p=preview

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to change an http call in a controller to a Service/factory pattern that accepts a parameter?

Currently, I have a controller making use of http.get, http.push and http.post methods within my AngularJS app. During my learning journey with AngularJS, I've discovered that it's more efficient to place http.get calls in service files. While I ...

having difficulty sorting items by tag groups in mongodb using $and and $in operators

I'm currently trying to execute this find() function: Item.find({'tags.id': { $and: [ { $in: [ '530f728706fa296e0a00000a', '5351d9df3412a38110000013' ] }, { $in: [ ...

Setting up Karma configuration to specifically exclude nested directories

When unit testing my Angular 2 application using Karma, I encountered an issue with my directory structure: └── src/ ├── index.js ├── index.js.text ├── index.test.js ├── README.txt ├── startuptest.js ...

Tips for creating a secure authentication system in an AngularJS application!

As a novice in the world of angularjs... After going through the documentation and completing a tutorial, I decided to experiment on my own which has helped me grasp things better. Now, I'm looking into creating a secure authentication system. The ...

Blazor compatibility issue with Bootstrap 5 carousel functionality

Check out the link below to view a carousel that is functioning with the help of jquery and bootstrap 4.3.1 (code lines 48,50). I attempted to switch to using Bootstrap 5 JS files for the carousel, but it did not work as expected due to issues with code li ...

divide a JSON list

Within this JSON array, there is a plethora of person data, each with their own specified type such as "hr" or "accounting". The structure of the JSON array is depicted below: { "0": { "id": "2", "name": "blabla", "type": "hr" ...

How can I utilize ng-repeat in AngularJS to iterate through an array of objects containing nested arrays within a field?

Here is the structure of an array I am working with: 0: {ID: null, name: "test", city: "Austin", UserColors: [{color: "blue"},{hobby:"beach"} ... ]} }... I am currently attempting to ng-repeat over this initial array in my code. However, when I tr ...

Troubleshooting issue with jest expect.any() failing to work with a custom class following migration from JavaScript to TypeScript

I recently made the switch to TypeScript in my project, and now some of my Jest tests are failing. It appears that the next function below is no longer being called with an AppError object, but with an Error object instead. Previously, the assertion expec ...

Struggling to properly render JSON data

Having trouble accessing specific elements in a script that merges local JSON files using AJAX? The script works fine in Chrome Console, but you can't reach certain elements like 'object.country'. Any suggestions on how to tackle this issue? ...

Having issues with array.push functionality in JavaScript

let chatConversations = new Array(); jQuery('.CChatWindow').each(function(){ if (jQuery(this).is(":visible") && jQuery(this).attr("data-conversationid") != 0) { alert(jQuery(this).attr("data-conversationid")); // returns 1 and ...

Is there a way to load an image asynchronously when the page loads and show a loading gif during the loading process?

My image tag displays a dynamically generated graph from the database. The loading time can vary significantly, sometimes only taking a second while other times it may take up to 6 or 7 seconds for the graph image to appear. I am looking for a way to sho ...

ion-select is experiencing display issues

I've been attempting to incorporate select options from the ionic framework, following the guidelines found here: http://ionicframework.com/docs/v2/api/components/select/Select/ <ion-item> <ion-label>Toppings</ion-label> <ion ...

Executing a Javascript function post AJAX page loading

I'm not a coding expert, so I hope my question is still clear. Essentially, I have an index.php page with filters (by project, year, month) that send variables to filterData.php after clicking submit. These variables are then used in SQL statements t ...

Creating a nested observable in Angular2: A step-by-step guide

Currently, I am exploring a new approach in my Angular2 service that involves using observables. The data source for this service will initially be local storage and later updated when an HTTP call to a database returns. To achieve this dynamic updating of ...

the failure of the second function to execute

Struggling with a particular issue. Two functions have been created as shown below: function limit_char_normal(textid, limit, infodiv){ var text = $('#'+textid).val(); var textlength = text.length; if (textlength > limit) { ...

Why does the return value of a function in Node.js and JavaScript sometimes appear as undefined?

I am completely stumped by this issue. I've been trying to figure it out, but so far, no luck.. this is the code snippet function part1(sql, controltime, headers_view, results_view, tmp){ var timerName = "QueryTime"; var request = ne ...

Looking for a way to make the spinner disappear in puppeteer while waiting?

What is the most effective way to wait for the spinner to vanish before clicking on a button? Check out this example ...

How come my div doesn't stick together with the other div while moving?

I am trying to figure out how to make the blue "shield" div cover the red "button" div after 3 clicks successfully. It seems like no matter what I do, the positioning is always off. I've played around with the randomplace variable, but nothing seems t ...

JavaScript issue: "No relay configuration found" error specifically occurs in Internet Explorer versions 7 and 8

Encountering issues with loading JavaScript only on specific pages in Internet Explorer. Safari, Firefox, and Chrome render the page correctly. Debugging revealed the following errors: 1) No relay set (used as window.postMessage targetOrigin), cannot send ...

Transform the MUI Typescript Autocomplete component to output singular values of a specific property rather than a complete object

When utilizing MUI Autocomplete, the generic value specified in onChange / value is determined by the interface of the object set in the options property. For instance, consider an autocomplete with the following setup: <Autocomplete options={top ...