How to access a DOM element in an Angular component template?
Hello! I'm new to Angular and TypeScript, and I'm having trouble accessing a DOM element inside a component.
I have a simple component that displays a button and a paragraph. I want to change the text in the paragraph and add a CSS class to it when the button is clicked. In regular JavaScript, I would use `document.getElementById('myParagraph')`, but in Angular this doesn't seem to work correctly.
Here's my current code:
```typescript
// my-component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-my',
template: `
Initial text
`, styleUrls: ['./my-component.css'] }) export class MyComponent { changeText() { // Trying to get the element by ID const pNo answers yet.
Log in to leave an answer
Blogs
Error Handling in Java: try/catch and Beyond — A Complete Guide
Testing in Go: Unit, Integration, E2E — A Complete Guide
Expression<Func<T>> vs Func<T>: The Secret to Optimization in C#
htmlentities() vs htmlspecialchars() in PHP: Full Breakdown with Examples