Try using ngFor within the insertAdjacentHTML method

When a user clicks, I dynamically attach an element inside a template like this:

this.optionValue = [];

youClickMe(){
  var moreput = '';
      moreput += '<select">';
        moreput += '<option *ngFor="let lup of optionValue">{{lup.name}}</option>';
      moreput += '</select>';
  var pElement = document.querySelector('.somewhereyoubelong');
      pElement.insertAdjacentHTML('beforeend', moreput);
}

My issue is that the {{lup.name}} is not displaying the actual value but rather as it is typed there. Does anyone know how to make it work correctly?

UPDATE:

I have attempted another approach, but encountered this error message:

const templating = '<p *ngFor="let sizeCat of sizeCategoryBySubCategory" [value]="sizeCat.id">{{sizeCat.name}}</p>';

const tmpCmp = Component({template: templating})(class {});
const tmpModule = NgModule({declarations: [tmpCmp]})(class {});

this._compiler.compileModuleAndAllComponentsAsync(tmpModule).then((factories) => {
  const f = factories.componentFactories[0];
  const cmpRef = f.create(this._injector, [], null, this._m);

  //cmpRef.instance.testText = 'B component';
  cmpRef.instance.sizeCategoryBySubCategory = [
    { id:1, name: 'a'},
    { id:2, name: 'b'},
  ];

  this._container.insert(cmpRef.hostView);
});

Property binding ngForOf not used by any directive on an embedded template

Answer №1

Angular does not recognize Angular-specific syntax like component or directive selectors and bindings for dynamically added HTML.

During compilation, Angular generates the necessary code for these elements in components. Anything that is not present in a component's template during compilation will be ignored by Angular.

One possible solution is to dynamically create and compile Angular components at runtime. For more information, please refer to How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

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

Ways to retrieve data from ngrx and display it in a component file

I have recently incorporated the ngrx library into my project to create a more dynamic model for handling data. After setting up my actions, reducers, and effects files, everything seems to be working as intended because I can observe the populated model w ...

Issues preventing Angular2 project from being operational

My angular 2 project was running smoothly on my ubuntu machine until I encountered this error. Strangely, just 5 minutes ago it was working fine. The issue arose after I ran another ionic2 project and now the angular project is throwing the following err ...

MaterialUI Divider is designed to dynamically adjust based on the screen size. It appears horizontally on small screens and vertically on

I am currently using a MaterialUI divider that is set to be vertical on md and large screens. However, I want it to switch to being horizontal on xs and sm screens: <Divider orientation="vertical" variant="middle" flexItem /> I h ...

Set up a global variable for debugging

Looking to include and utilize the function below for debugging purposes: export function debug(string) { if(debugMode) { console.log(`DEBUG: ${string}`) } } However, I am unsure how to create a globally accessible variable like debugMode. Can this be ...

Learn how to send error logs from an Angular frontend to the backend using a custom API or any other method to store them in the Serilog table in MSSQL

Is there a way to log errors from an Angular frontend to a backend using a custom API or any other method that can send the data to Serilog's SQL sink table in MSSQL? My application utilizes multiple APIs from various third-party resources, and I need ...

Troubleshooting: Angular 2+ Observable not updating the UI

After triggering login() or logout() in auth.service.ts from the login.ts component following a Facebook popup, the value of isAuth remains false in the home view. However, if I include console.log(this.isAuth) within the subscribe() function, it updates c ...

The issue of losing context when using Papaparse with an Angular 4 function

Check out this block of code httpcsv2Array(event) { var gethttpcsv = Papa.parse('https://docs.google.com/spreadsheets/d/e/yadyada/pub?output=csv', { download: true, header: true, ...

Can you explain the distinction between any[] and [] in TypeScript?

Here is an example that successfully works: protected createGroups(sortedItems: Array<TbpeItem>): any[] { let groups: any[] = []; return groups; } However, the second example encounters a TypeScript error: type any[] not assignable to ...

New post: "Exploring the latest features in Angular

Looking for help with integrating Angular and SpringREST to fetch data from the backend? Here's my situation: I need to retrieve a JSON string from the backend using a POST request, send it to my site's hosted link, and display it on the user int ...

Troubleshooting Angular 6: Issues with Route Guards not functioning as expected

Striving to enhance frontend security by restricting access to specific IDs. The goal is to redirect anyone trying to access routes other than /login/:id to a page-not-found error message if not already logged in, but encountering some issues. Below are t ...

Unable to locate the Chart object within the chartjs-plugin-labels.js file

Hello there, I am currently working on an Angular project where I want to incorporate a chart plugin. To achieve this, I executed the following commands: npm install angular2-chartjs npm install chartjs-plugin-labels Following that, I imported it into my ...

Can ES6 class getters, setters, and private properties be utilized in TypeScript with an interface?

I'm currently using TypeScript and trying to figure out how to implement an interface in a class that utilizes ES6 getters and setters. Is it even possible? When I use the code below, errors are highlighted in the class. For instance: (property) S ...

Bootstrap Modal Fails to Display Image Within Its Container

I'm currently working on my e-commerce website, where I'm attempting to implement a feature that allows users to view product images in a modal for a closer look. The product page contains a list of images, and when a user clicks on one, it shoul ...

TS type defined by JS constants

I am currently working on a project that involves both JavaScript and TypeScript. I am trying to find a solution to reduce code duplication when using JavaScript string constants by converting them into TypeScript types. For example, let's say I have ...

Divide Angular component unit test involving asynchronous tasks

One of my components is responsible for fetching data from a server using a service and then displaying it. I have created a test for this component which ensures that the data is loaded correctly: ... it('Should contain data after loading', as ...

Angular2-starter configuration setup with environment-based variables (such as .env or .conf) for testing and production settings

Frameworks like PHP Laravel often include files for local configuration, separate from dev, test, and production environments. How can a configuration file be provided for an angular-starter project that contains all local environment variable values (su ...

I am experiencing difficulty with VS Code IntelliSense as it is not displaying certain classes for auto-import in my TypeScript project

I'm currently working on a project that has an entrypoint index.ts in the main folder, with all other files located in src (which are then built in dist). However, I've noticed that when I try to use autocomplete or quick fix to import existing ...

Encountering an error in Angular: Module '@angular-devkit/core' not found

Whenever I run ng serve on my fresh Angular project, I encounter the following error: module.js:538 throw err; ^ Error: Cannot find module '@angular-devkit/core' What could be causing this issue? ...

Embedding and authorization

I'm currently working on incorporating the OHIF DICOM viewer into an iFrame within an Angular project. However, it seems that Google accounts are not allowed to function within an iFrame (OAuth is required with the OHIF viewer). Is there a way to enab ...

Circular reference in Angular/TypeScript

I encountered a circular dependency issue in my Angular project and tried various solutions, including exporting all dependent classes from a "single file" as suggested here. Unfortunately, this approach did not work for me. I then explored other solutions ...