Complete the code to create a standalone Angular component.
import { Component } from '@angular/core'; @Component({ selector: 'app-hello', template: `<h1>Hello, Angular!</h1>`, standalone: [1] }) export class HelloComponent {}
The standalone: true flag makes the component standalone, which is a modern Angular pattern for simpler module management.
Complete the code to inject a service using Angular's new inject() function.
import { Component, inject } from '@angular/core'; import { LoggerService } from './logger.service'; @Component({ selector: 'app-log', template: `<p>Check console for logs</p>`, standalone: true }) export class LogComponent { logger = [1](LoggerService); constructor() { this.logger.log('Component created'); } }
The inject() function is the modern way to get a service instance inside a component or function without constructor injection.
Fix the error in the Angular signal usage to track a counter value.
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', template: `<button (click)="increment()">Count: {{ count() }}</button>`, standalone: true }) export class CounterComponent { count = [1](0); increment() { this.count.set(this.count() + 1); } }
The signal function creates a reactive signal in Angular. It must be lowercase and imported from '@angular/core'.
Fill both blanks to create a reactive effect that logs count changes.
import { Component, signal, effect } from '@angular/core'; @Component({ selector: 'app-reactive-log', template: `<button (click)="increment()">Count: {{ count() }}</button>`, standalone: true }) export class ReactiveLogComponent { count = signal(0); constructor() { effect(() => { console.log('Count is', [1]()); }); } increment() { this.count.[2](this.count() + 1); } }
The effect runs a function that accesses the signal count() to track changes. To update the signal, use set.
Fill all three blanks to create a standalone Angular component with a signal and effect that updates a message.
import { Component, signal, effect } from '@angular/core'; @Component({ selector: 'app-message', template: `<p>{{ message() }}</p><button (click)="changeMessage()">Change</button>`, standalone: [1] }) export class MessageComponent { message = signal('Hello'); constructor() { effect(() => { console.log('Message:', [2]()); }); } changeMessage() { this.message.[3]('Hi there!'); } }
The component must be standalone (true). The effect accesses the signal message(). To update the signal, use set.