Using Angular to dynamically access component properties

Seeking assistance with creating dynamic Tabs in TabView of PrimeNG. The components are displaying properly, but I am unsure how to access their properties.

I am following the guidelines provided at https://angular.io/guide/dynamic-component-loader and using the example shown in the bottom part at https://angular.io/api/core/ViewContainerRef#createComponent.

The bookingView.html file contains:

<p-tableView>
  <p-tabPanel *ngFor="let tab of tabs">
    <ng-container *ngComponentOutlet="tab.component"></ng-container
  </p-tabPanel>
</p-tableView>

The panel definitions are stored in a Service called bookingService.ts:

getAllInitialTabs() {
   return [
    {
      title: "BookingOverview",
      component: BookingOverviewComponent,
    },
    {
      title: "BookingDetails for US",
      component: BookingDetailsComponent,
    },
    {
      title: "BookingDetails for GER",
      component: BookingDetailsComponent,
   }
] as {title: string, component: Type<any>}[];

In my main component, I load the server data into an array of tabs:

ngOnInit() {
  this.tabs = this.bookingService.getAllInitialTabs();
}

It seems to be working fine, showing all Tabs in the PrimeNG TabView.

However, I am struggling to access the components to set/get values and receive callbacks when actions are executed.

I have tried:

vcr = inject(ViewContainerRef);

setFields() {
   const compRef = this.vcr.createComponent(this.tabs[0].component);
   compRef.changeDetectorRef.detectChanges();
   (<BookingOverviewComponent>compRef.instance).maxAmount = 2000;
}

Unfortunately, the view does not update, and the value of maxAmount remains unchanged.

Any suggestions on how to access the created component and populate it with data? Also, how can I capture information about user interactions such as Button Click events?

Answer №1

Consider revising the sequence as follows:

(<BookingOverviewComponent>compRef.instance).maxAmount = 2000;
compRef.changeDetectorRef.detectChanges();

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

Storing Boolean values in MongoDB with Express JS: A Step-by-Step Guide

I am encountering an issue where a Boolean value that I am trying to store in Mongodb always returns false. Below is the schema of my database: const UserSchema = new Schema({ name: String, password: { type: String, required: true }, isAdmi ...

Create a list of items with checkboxes next to each that can be repeated using PdfMake

Incorporating pdfMake into my project, I am trying to display text next to an image and replicate this section in my docDefinition. The issue arises when I attempt to repeat this part using the following code snippet: { columns: [ { ...

Experimenting with code within a window event listener function

I have a component in my AngularJS application that is functioning correctly. However, when it comes to test coverage, everything after the 'window.addEventListener('message',' part is not being covered. Should I create a mock object f ...

Why is it that when I use require.js, all my modules appear to be loading at once when the page first

During the development of my single-page backbone app using requirejs, I encountered an issue when deploying to our beta server. The initial page load took around 20 seconds as it had to fetch all the necessary scripts. I initially believed that this dela ...

Using renderProps in combination with TypeScript

I've encountered an issue while trying to convert my React project to TypeScript, specifically with the login component that uses react-google-login. The error I'm facing is related to renderProps: Overload 1 of 2, '(props: { component: El ...

calling object functions using additional parentheses

After reviewing the passport.js documentation, I came across this piece of code: app.get('/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err); } if (!u ...

How can I conceal an element upon page load using Ionic framework?

index.component.ts initialize() { // Execute necessary functions after loading this.index.ready().then(() => { if(this.index.is('core')){ // this.menu.enable(false, 'mobileOnlyPages'); }else{ ...

Refresh tab controllers in Angular JS on every click event

Is there a way to refresh the tab controller every time a tab is clicked? Here's the current code: $scope.tabs = [ { id: 'tab1', title: 'tab1', icon: 'comments', templateUrl: 'tab1/tab1.tpl.html&ap ...

Adding array elements to a JavaScript object

I find myself in a rather unique predicament that I'm struggling to navigate. I have come across some data structured as follows. Please bear with me if I use any incorrect terminology, as I am relatively new to this. usersByName: { "tester&q ...

The concept of recursively exporting modules in Node.js modules

Looking for a way to recursively export all .hbs files in a NodeJS 14+ project main JS. I attempted the following: module.exports = () => ({ partial : __dirname + "/../partial/**.hbs", helper : __dirname + "/../helper/*.js" } ...

Managing dates in my Angular 2 application using Typescript

Currently, I am in the process of generating test data for my views before initiating API calls to the API application. Within a service in my Angular 2 application, I have defined an interface as follows: export interface amendmentBookings { booking ...

Using Typescript to extract elements from one array and create a new array

I have a set of elements "inputData" , and it appears as follows : [{code:"11" , name= "test1" , state:"active" , flag:"stat"}, {code:"145" , name= "test2" , state:"inactive" , flag:"pass"}, {code1:"785" , name= "test3" , state:"active" , flag:"stat"}, .. ...

Identify the nature of the output received after dispatching

I'm developing a functional component within the realm of Redux, and I have configured it to return specific values for a particular action. The new value being returned is derived from a Promise, meaning that if the type is designated as "Ival," the ...

What is the most efficient way to use the $slice operator on a highly nested array in mongoose

I am currently working on slicing a deeply nested array. To illustrate, consider the following structure. I aim to slice this array for pagination purposes. {messages: [{ message: { members: [ {example: object, blah: blah}, {example2: object2, blah2: blah ...

Capture Video on iOS devices using the MediaRecorder API and display it using HTML5 Video Player

Issue: I am facing an issue where I cannot record video or get a video stream from the camera on iOS through my Angular web application built using ng build. Investigation: In my investigation, I explored various websites that discuss Apple iOS features ...

How can I automatically select the first radio button using Angular?

I am facing an issue with displaying a dynamic list of radio buttons, where the first one always needs to be pre-selected. Initially, I used: [checked]="i === 0" Then, when I added: [(ngModel)]="item.modifType" The first radio button was no longer sele ...

What is the best way to determine the value of margin-left in inline CSS using jQuery?

Here is the code snippet I am working with: <div class="ongoing" style="width: +728px; margin-left: +12480px"> I need to extract the value of the margin-left property for scrolling purposes. The technique I used to retrieve the width value does no ...

Incorporating Chip into a Material-UI DataGrid column

I'm having trouble displaying data of a specific column inside a chip. I attempted to use the Chip component in my code: StackBlitz Demo Web Link: Live Demo I tried to incorporate it using: import Chip from '@mui/material/Chip'; but c ...

Exploring end-to-end testing with NestJS and Guards

I'm trying to test an endpoint called /users using nestjs, but I encountered some errors. I'm unsure how to fix the issues and make the test pass with a guard. First Issue Nest is unable to resolve dependencies of the UserModel (?). Please en ...

Is it feasible to use Angular2 in conjunction with ui-calendar?

Entering the fascinating world of Angular 2 has me feeling like a beginner again. My team and I had been utilizing angularjs with ui-calendar in our project, but now we're transitioning to Angular 2 due to new production requirements. The challenge ar ...