Ausnahme gefangen: SSL certificate problem: certificate is not yet valid 📌 Create an analog clock using RxJS and Angular standalone components

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Create an analog clock using RxJS and Angular standalone components


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Introduction

This is day 2 of Wes Bos's JavaScript 30 challenge where I create an analog clock that displays the current time of the day. In the tutorial, I created the components using RxJS, custom operators, Angular standalone components and removed the NgModules.

In this blog post, I describe how to create an observable that draws the hour, minute and second hands of an analog clock. The clock component creates a timer that emits every second to get the current time, calculates the rotation angle of the hands and set the CSS styles to perform line rotation.

Create a new Angular project

ng generate application day2-ng-and-css-clock

Bootstrap AppComponent

First, I convert AppComponent into standalone component such that I can bootstrap AppComponent and inject providers in main.ts.

// app.component.ts

import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ClockComponent } from './clock';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [
    ClockComponent
  ],
  template: '<app-clock></app-clock>',
  styles: [`
    :host {
      display: block;
    }
  `],
})
export class AppComponent {
  constructor(titleService: Title) {
    titleService.setTitle('Day 2 NG and CSS Clock');
  }
}

In Component decorator, I put standalone: true to convert AppComponent into a standalone component.

Instead of importing ClockComponent in AppModule, I import ClockComponent (that is also a standalone component) in the imports array because the inline template references it.

// main.ts

import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

bootstrapApplication(AppComponent).catch(err => console.error(err));

Second, I delete AppModule because it is not used anymore.

Declare Clock component

I declare standalone component, ClockComponent, to create an analog clock. To verify the component is a standalone, standalone: true is specified in the Component decorator.

src/app
├── app.component.ts
└── clock
    ├── clock.component.ts
    ├── clock.interface.ts
    ├── custom-operators
    │   └── clock.operator.ts
    └── index.ts

clock-operators.ts encapsulates two custom RxJS operators that help draw the clock hands of the analog clock. currentTime operator returns seconds, minutes and hours of the current time. rotateClockHands receives the results of currentTime and calculate the rotation angle of the hands.

// clock.component.ts

import { AsyncPipe, NgIf } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { timer } from 'rxjs';
import { currentTime, rotateClockHands } from './custom-operators/clock.operator';

@Component({
  selector: 'app-clock',
  standalone: true,
  imports: [
    AsyncPipe,
    NgIf,
  ],
  template: `
    <div class="clock" *ngIf="clockHandsTransform$ | async as clockHandsTransform">
      <div class="clock-face">
        <div class="hand hour-hand" [style.transform]="clockHandsTransform.hourHandTransform"></div>
        <div class="hand min-hand" [style.transform]="clockHandsTransform.minuteHandTransform"></div>
        <div class="hand second-hand" [style.transform]="clockHandsTransform.secondHandTransform"></div>
      </div>
    </div>
  `,
  styles: [...omitted due to brevity...],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ClockComponent {
  readonly oneSecond = 1000;

  clockHandsTransform$ = timer(0, this.oneSecond)
    .pipe(
      currentTime(),
      rotateClockHands(),
    );
}

ClockComponent imports NgIf and AsyncPipe because the components uses ngIf and async keywords to resolve clockHandsTransform$ observable. clockHandsTransform$ is an observable that is consisted of the CSS styles to draw hour, minute and second hands. The observable is succinct because the currentTime and rotateClockHands custom operators encapsulate the logic.

Create RxJS custom operators

It is a matter of taste but I prefer to refactor RxJS operators into custom operators when observable has many lines of code. For clockHandsTransform$, I refactor map into custom operators and reuse them in ClockComponent.

// clock.operator.ts

export function currentTime() {
    return map(() => { 
        const time = new Date();
        return { 
            seconds: time.getSeconds(),
            minutes: time.getMinutes(),
            hours: time.getHours()
        }
    });
}

currentTime operator gets the current time and calls the methods of the Date object to return the current second, minutes and hours.

// clock.operator.ts

function rotateAngle (seconds: number, minutes: number, hours: number): HandTransformations { 
    const secondsDegrees = ((seconds / 60) * 360) + 90;
    const minsDegrees = ((minutes / 60) * 360) + ((seconds / 60) * 6) + 90;
    const hourDegrees = ((hours / 12) * 360) + ((minutes / 60) * 30) + 90;

    return { 
        secondHandTransform: `rotate(${secondsDegrees}deg)`,
        minuteHandTransform: `rotate(${minsDegrees}deg)`,
        hourHandTransform: `rotate(${hourDegrees}deg)`,
    }
}

export function rotateClockHands() {
    return function (source: Observable<{ seconds: number, minutes: number, hours: number }>) {
        return source.pipe(map(({ seconds, minutes, hours }) => rotateAngle(seconds, minutes, hours)));
    }
}

currentTime emits the results to rotateClockHands and the rotateClockHands operator invokes a helper function, rotateAngle, to derive the CSS styles of the hands.

Finally, I use both operators to compose clockHandsTransform$ observable.

Use RxJS and Angular to implement observable in clock component

// clock.component.ts
clockHandsTransform$ = timer(0, this.oneSecond)
    .pipe(
      currentTime(),
      rotateClockHands(),
    );
  • timer(0, this.oneSecond) - emits an integer every second
  • currentTime() - return the current second, minute and hour
  • rotateClockHands() - calculate the rotation angle of second, minute and hour hands

This is it, we have created a functional analog clock that displays the current time.

Final Thoughts

In this post, I show how to use RxJS and Angular standalone components to create an analog clock. The application has the following characteristics after using Angular 15's new features:

  • The application does not have NgModules and constructor boilerplate codes.
  • In ClockComponent, I import NgIf and AsyncPipe rather than CommonModule, only the minimum parts that the component requires.

This is the end of the blog post and I hope you like the content and continue to follow my learning experience in Angular and other technologies.

Resources:

...



📌 Create an analog clock using RxJS and Angular standalone components


📈 109.33 Punkte

📌 Create a drum kit using RxJS and Angular standalone components


📈 78.49 Punkte

📌 Text to speech tutorial using RxJS and Angular


📈 44.7 Punkte

📌 Exploring the Pros and Cons of Standalone Components in Angular


📈 41.5 Punkte

📌 CVE-2023-26116 | angular angular.copy redos (SNYK-JS-ANGULAR-3373044)


📈 40.14 Punkte

📌 Implementing Standalone Components in Angular 15


📈 39.71 Punkte

📌 Standalone components in Angular


📈 39.71 Punkte

📌 How to install Bootstrap 5 in Angular 17... Standalone components Including css,js & icons.


📈 39.71 Punkte

📌 Infinite Scrolling the Angular 6 and RxJS Way!


📈 39.57 Punkte

📌 Sub-RFC 4 for Angular Signals sparks interesting discussion started by RxJS author — Ben Lesh


📈 37.78 Punkte

📌 Episode 24/07: Angular 17.2, optional RxJs


📈 37.78 Punkte

📌 Accelerate Angular App Development with create-angular-auth-nav


📈 34.22 Punkte

📌 How to Run Angular Apps Using Angular CLI and PM2


📈 33.67 Punkte

📌 GitHub - saivert/alt-analog-output: Enable alt analog device on some Realtek (Intel HDA) sound devices


📈 32.95 Punkte

📌 This week in KDE: the Analog Clock changes color – Adventures in Linux and KDE


📈 32.62 Punkte

📌 Using Angular’s EventEmitter to Share Data Between Child and Parent Components


📈 31.98 Punkte

📌 Bridging Analog to Angular with esbuild and Vite


📈 31.64 Punkte

📌 World Clock Deluxe 4.19.1 - World time clock and meeting planner.


📈 30.5 Punkte

📌 SPARTAN. Type-safe Angular full-stack development powered by Analog.


📈 29.86 Punkte

📌 Create a Simple Digital Clock Using HTML, CSS and JavaScript


📈 28.73 Punkte

📌 Clock blocker: Woman sues bosses over fingerprint clock-in tech


📈 28.72 Punkte

📌 Second Clock 1.0.0 - Second clock for a different time zone in your menu bar.


📈 28.72 Punkte

📌 The Clock 4.9 - World clock, meeting planner, calendar.


📈 28.72 Punkte

📌 Angular, React, Vue and Co: Peacefully United Thanks To Web Components and Micro Apps


📈 28.64 Punkte

📌 Angular, React, Vue and Co: Web Components and Mini Apps


📈 28.64 Punkte

📌 Updates from the Angular Team and new Angular 17 features!


📈 28.55 Punkte

📌 Angular Addicts #24: Angular 17.3, Signals and unit testing best practices, Storybook 8 & more


📈 28.55 Punkte

📌 How to make a clock using html , JavaScript and CSS and deploy it using firebase


📈 28.19 Punkte

📌 How do I test and mock Standalone Components?


📈 28.12 Punkte

📌 Angular 14.2 bringt Best Practices im Umgang mit Bildern und Standalone-Routing


📈 28.03 Punkte

📌 Angular Standalone in SSR: update


📈 28.03 Punkte

📌 Create Digital Clock Using JavaScript


📈 26.95 Punkte











matomo