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

Error: the search variable is not defined

function sorting(type) { $("#parentDiv").empty(); $.getJSON("example_data.json", ({ Find })); function Locate(a, b) { return (a[Find.type] < b[Find.type]) ? -1 : (a[Find.type] > b[Find.type]) ? 1 : 0; }; } The example_data.j ...

What could be causing a momentary 404 error when I click on a next.js `Link` that connects to an API endpoint before redirecting to the intended page?

Hello there, I recently followed a tutorial on adding authentication with Passport to Next.js from this resource: https://auth0.com/blog/next-js-authentication-tutorial/ However, I encountered a minor issue. When I click the "/login" Link in my Next.js ...

What steps do I need to follow in order to incorporate and utilize an npm package within my Astro component

I have been working on integrating KeenSlider into my project by installing it from npm. However, I am encountering an error message that says Uncaught ReferenceError: KeenSlider is not defined whenever I try to use the package in my Astro component. Belo ...

Utilize the sortable script for dynamically loaded items via AJAX

For the drag and drop feature on my website, I am using jQuery sortable. I have a button that displays results with items on the screen. These items can be dragged and dropped into various sections. The issue I'm facing is that if I include the sort ...

How to return a variable to Express when clicking a button?

Imagine having a list of 10 elements, each with a unique id as their name, and the user has the ability to add more items to the list at any time. If I were to click on an element, my goal is to have Express retrieve the id of that specific element. If it ...

Issue specifically with Android 6 WebView - jQuery 1.9.1 where a RangeError is thrown due to the call stack size being exceeded

An error message "Uncaught RangeError Maximum call stack size exceeded" is causing a web application to fail in the jQuery-1.9.1 extend() function, but strangely it only occurs on Android 6. The application runs smoothly on all other platforms such as Des ...

SignalR does not include cookies in the connection request

While working on a web application using ASP.NET Web API and SignalR, I encountered an issue that is proving to be quite challenging despite my understanding of HTTP. Currently, I have successfully set a cookie on all AJAX requests to the API, and subsequ ...

Node js overlooking non-packaged middleware

I encountered an issue with middleware that used to be included in NODE.js but is no longer bundled. I followed the instructions provided here: Node js Error: Most middleware (like session) is no longer bundled with Express and must be installed separatel ...

Observable in Angular 2 that emits numbers

Currently, I am working on developing a countdown timer using AngularJS 2 that starts from n=60 seconds (i.e. hh:min:sec) My implementation includes the following code: countDown: Observable<number>; count = 60; constructor() { this.countDown = O ...

Is it possible to adjust the timezone settings on my GraphQL timestamp data?

I've come across a lot of helpful information regarding the usage of Date() and timezones, but something seems to be off. In my GraphQL setup (sourcing from Sanity), I have configured it to use formatString in this manner: export default function Minu ...

It is not possible to use Date.now within passport.js

When creating a user via Facebook on my Node.js website using passport, I want to assign the register date. In my Mongoose Schema, I can use the following for the local provider: regisDate: { type: Date, default: Date.now }, However, when it co ...

Utilizing a single component for various purposes with diverse tags in react

I am faced with the challenge of creating two almost identical components, Component A: const ClaimedLabel = ():React.Element => ( <div tw=""> <p tw="">Some Text here</p> <div tw=""> <Icon ...

The absence of the Angular property has been detected

Let's say you have the following model in your application. export class Instructor{ firstname:string; lastname:string; } export class Course { ID: number; title: string; crn: string; instructor:Instructor; } In order to reset a form, you can us ...

Tips for making jQuery maphilight function properly?

Can someone assist me with Mapilight? I have been trying to get it to work but no success so far. Here are the script links in the head section of my HTML. <script type="text/javascript" src="/js/jquery.js"></script> <script type="text/ja ...

If the input is marked as checked, apply a class to the corresponding HTML element

I need to manipulate the .form-group class by adding it if the nearest hotelObj element is checked, and removing it when hotelObj is unchecked. Instead of using addClass(), I prefer to utilize css(). $(".form-group").click(function() { if ($(this).ch ...

Struggling to understand the implementation of webpack's require.context() method

I'm currently working on an AngularJS project with webpack, and I'm looking for a way to import all the .js files in my project into webpack without manually adding each file path. Upon reviewing the webpack documentation, I came across the requi ...

What could be causing the code to produce 4 elements instead of the expected 2?

I'm trying to understand why the code above is creating four new paragraphs instead of just two. Can someone please explain what exactly happens in the $("p").before($("p").clone()); part? <!DOCTYPE html> <html> <head> <script ...

Adding the AJAX response to the specified element's ID

I'm having trouble inserting radio buttons into a <div> on my page using an AJAX response. The code I have looks like this: // AJAX form for updating tests after category selection $('#categories').change(function(){ category_id ...

What is the process for converting language json files into different languages?

I'm currently using ngx-translate to localize an Angular app. With over 20 languages that need translation, I am in search of a tool that can efficiently translate the language json files. While I did come across a helpful website, it proved to be ti ...

Ionic JavaScript function runs independently of promise resolution

Within my ngOnInit on a page, I have a method that is meant to fetch data. However, it seems like the other methods in the chain are executing before the fetch data process is complete. Here's a snippet of the code: ngOnInit() { this.devicesinfo.fetc ...