Retrieving the component's values when utilizing the `<ng-content>` directive

Seeking a technique for accessing the values of a component when utilizing <ng-content>:

import {Component} from '@angular/core';

@Component({
  selector: 'home-page',
  template: `<person-box>{{name}}</person-box> <!-- something similar -->`
})
export class HomePageComponent {
  // code missing?
}

Code for the component:

import {Component} from '@angular/core';

@Component({
  selector: 'person-box',
  template: `<div style="background-color: blue;"><ng-content></ng-content></div>`
})
export class PersonBoxComponent {
  name = 'Katharina Muster';
}

(The example above is quite simple.)


Using @ViewChild makes it function properly:

import {Component} from '@angular/core';

@Component({
  selector: 'home-page',
  template: `<person-box>{{box.name}}</person-box>`
})
export class HomePageComponent {
  @ViewChild(PersonBoxComponent) box: PersonBoxComponent;
}

Answer №1

@ViewChild(UserCardComponent) card:UserCardComponent;
  
  afterViewInit() {
    console.log(this.card);
  }
  

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

Issue with PrimeNG overlaypanel displaying error message "Functionality of this.engine.setProperty is not available"

After importing OverlayPanelModule as @NgModule in parent.module.ts, I added the following code in child.component.html: <p-overlayPanel [dismissable]="false" #overlay> Content </p-overlayPanel> However, upon testing, I encountered the error ...

It seems that there is an issue with Ionic v4 and Angular when trying to retrieve the inner text of ion-slide elements in loop

I have a set of statuses displayed in <ion-slide> using the *ngFor directive to loop through a specified array: status: string[] = [ 'Active', 'Inactive', 'Ended' ]; @ViewChild('slideWithNav') slides: Io ...

Can you explain the significance of the ellipsis in @NGRX for Angular?

Can you explain the significance of these three dots and why they are necessary? export function leadReducer(state: Lead[]= [], action: Action { switch(action.type){ case ADD_LEAD: return [...state, action.payload]; case R ...

Angular date selection with a range of plus two days, factoring in the exclusion of weekends

I am currently using a mat date picker range with specific logic. The minimum date that a user can select on the calendar is set to + 2 days. For example, if today's date is July 20, 2022, the minimum selectable date would be July 22, 2022. However, ...

Guide to Integrating BLK Theme into an Angular CLI Project

I've recently set up an Angular project using CLI and I am interested in integrating the BLK theme by Creative Tim into this project. However, the only available option from Creative Tim is to download a pre-existing project and build upon that framew ...

Unable to fake a fetch request using the 'fetch-mock-jest 1.5.1' library

I am attempting to simulate a fetch call using thefetch-mock-jest library, but the code continues to try accessing the remote address and ultimately fails with the error message FetchError: request to https://some.domain.io/app-config.yaml failed, reason: ...

Using the Angular Slice Pipe to Show Line Breaks or Any Custom Delimiter

Is there a way in Angular Slice Pipe to display a new line or any other delimited separator instead of commas when separating an array like 'Michelle', 'Joe', 'Alex'? Can you choose the separator such as NewLine, / , ; etc? ...

Angular 2 Mastering the Art of Saving State in Routing with Precision

I have been working on a search page that retains users' search criteria even after navigating away from the page. I came across a helpful resource at that enabled me to achieve this functionality successfully. However, I encountered an issue where ...

How can I adjust the size and width of the autofocus cursor inside an input box using Angular?

How can I modify the height and width of the cursor in an input field with auto focus? app.component.html <input class="subdisplay" [ngModel]="input | number" type="text" (keyup)="getValue(box.value)" name ...

Oops! Looks like there's an issue with the route configuration for ''. Make sure to include one of the following: component, redirectTo, children, or loadChildren in order to validate the

I've been facing an issue while setting up a project with angular routing. The project is only displaying the contents of index.html and not app.component.html. Upon inspection, I noticed that the body tag just has <app-root></app-root> in ...

Tips for utilizing multiple components in Angular 2

I am a beginner in angular2 and I am attempting to utilize two components on a single page, but I keep encountering a 404 error. Here are my two .ts files: app.ts import {Component, View, bootstrap} from 'angular2/angular2'; import {events} f ...

Update to Angular 6 and experience issues with the Test functionality

At first, I started a project with Angular 5. As I progressed, I made the decision to upgrade to Angular 6. After the upgrade to Angular 6, I made some necessary adjustments to the code. However, when running the Test, I encountered an issue. https://i. ...

Angular 8 seems to be disregarding SetTimeout

When executing the following code snippet: this.logger.warn('start'); setTimeout(() => {},3000); delay(3000); this.logger.warn('start2'); The output displays enter image description here Blockquote: ngx-logger.js:596 2020-11-19T13 ...

How to access class type arguments within a static method in Typescript: A clever solution

An issue has arisen due to the code below "Static members cannot reference class type parameters." This problem originates from the following snippet of code abstract class Resource<T> { /* static methods */ public static list: T[] = []; ...

Issues with Angular ChartJS where the minimum value for scales and callback functions are not functioning as

I am encountering an issue while using ChartJS line chart in my Angular 9 application. I am attempting to adjust the Y axes of my chart so that they start from 0 (instead of the minimum value) and include a '%' symbol after each value. Below is a ...

The Redux Toolkit Slice is encountering an issue where it generates the incorrect type of "WritableDraft<AppApiError> when the extraReducer is

I defined my initial state as MednannyAppointments[] for data and AppApiError for error. However, when I hover over state.error or state.data in my extraReducer calls, the type is always WritableDraft. This behaviour is confusing to me. Even though I have ...

Ensuring that Vue3 Typescript app focuses on input element within Bootstrap modal upon opening

I am facing a challenge with setting the focus on a specific text input element within a modal dialog. I have tried various methods but none seem to achieve the desired outcome. Here is what I have attempted: Attempt 1: Using autofocus attribute. <inpu ...

Utilizing the RxJs map operator to update the attributes of multiple objects within an Angular collection

Inside my angular component, I have the following properties: memberInfoLists$: Observable<MemberInfo[]>; total$: Observable<number>; memberPhotoUrl: string = environment.memberPhotoUrl; memberDefaultPhoto: string = environment.defaultPersonPho ...

How can you determine the number of times a particular digit appears around a specific position in a TypeScript array

(Utilizing Typescript for assistance) I am looking to determine the number of occurrences of the digit 2 surrounding a specific position within an array. The function takes in the position coordinates as parameters - param 1 for row and param 2 for column. ...

How to include an image in a file type using Angular 2 and TypeScript

Is it possible to assign an image to a file type variable in Angular 2? I attempted the following approach: private file:File; setImage(){ this.file = "../assets/image/image.jpg" } Unfortunately, this method did not succeed. ...