0

I'm trying to create a component which is dependent on a configuration json file. It's content is dynamic so I need to add the elements dynamically.

I have the following lines in my controller for example:

const item: HTMLElement = this.renderer.createElement('div');
this.renderer.addClass(item, 'item');

This will generate a simple div:

<div class="item"></div>

How can I generate it with attribute / event binding, so it could be something like this:

<div class="item" [style]="property1" (click)="functionName()"></div>

Edit:

To be more specific, I'd like to generate this structure:

<div class="item">
    <div class="toggler" (click)="onTogglerClick(3)">accessories</div>
    <navbar-svg-caret></navbar-svg-caret>

    <div class="menu" [style]="visibility[3]">
        <div class="item">face masks</div>
        <div class="item">beanies</div>
        <div class="item">embroidered beanies</div>
        <div class="item">bucket hats</div>
        <div class="item">caps</div>
        <div class="item">neck sleeves</div>
        <div class="item">socks</div>
    </div>
</div>
2
  • Is it always a div with class, style and a click function? Commented Mar 24, 2021 at 7:55
  • That was just an example. Usually it's a div with a click event for a dropdown toggle and a div with style for it's corresponding dropdown menu. Commented Mar 24, 2021 at 7:57

1 Answer 1

1

How about *ngFor with JSON object.

It's bit tricky to bind the events for dynamically created DIV.

<span *ngFor="let object of yourObject">
     <div class="{{object.class}}" [style]="object.property1" (click)="functionName(object)"> {{object.lable}} </div>
</span>

Happy Coding.. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.