1

The following is a custom component:

<app-variable-component-1></app-variable-component-1>

So my idea is as the user changes the input i.e clicks or select option then the current selected value will be stored in a property inside the parent component.

Like this:

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

@Component({
  selector: 'app-parent-component',
  templateUrl: './parent-component.component.html',
  styleUrls: ['./parent-component.component.less']
})
export class ParentComponentComponent implements OnInit {

currentOption = 1

  constructor() { }

  ngOnInit(): void {
  }

onSelectOption(option) {
this.currentOption = option
}

}

using this property I want to change the component dynamically. I know I could have used *ngIf for each item like this:

<app-variable-component-1 *ngIf="currentOption === '1'"></app-variable-component-1>
<app-variable-component-2 *ngIf="currentOption === '2'"></app-variable-component-2>
<app-variable-component-3 *ngIf="currentOption === '3'"></app-variable-component-3>

But instead I want to shorten the code something like this:

<app-variable-component-{{currentOption}} ></app-variable-component-{{currentOption}}>

Is something similar possible in angular?

2
  • You can use ngSwitch, but what you have there is not possible Commented Sep 25, 2020 at 19:50
  • is there anything similar that I can do?? Commented Sep 25, 2020 at 19:52

2 Answers 2

1

what you have is not doable, you can use ngSwitch though to clean it up a bit:

<div [ngSwitch]="currentOption">
  <app-variable-component-1 *ngSwitchCase="'1'"></app-variable-component-1>
  <app-variable-component-2 *ngSwitchCase="'2'"></app-variable-component-2>
  <app-variable-component-3 *ngSwitchCase="'3'"></app-variable-component-3>
</div>

relying on selector names like that is generally not going to scale very well either way and will be difficult to maintain in a larger app.

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

5 Comments

it's not at all repetition of code. It's a switch statement.
if this is possible .. that willl be great : <app-variable-component-{{currentOption}} ></app-variable-component-{{currentOption}}>
as I said, it is not possible nor would it even be advisable. You would have a difficult to maintain and error prone application.
yeah... but this is more simpler ... right.... is there a way to create custom function or directive to achive like this <app-variable-component-{{currentOption}} ></app-variable-component-{{currentOption}}>
For the final time. NO, it is not possible.... it is less keystrokes, but it is not simpler nor is it better practice. You would have no way of checking your templates for validity as the variable could change or the selectors could change. This makes your application prone to breaks or failures. less code is not strictly better if it introduces opportunity for errors. The only other thing you could really consider doing is abstracting this switch into a single app-variable-switch component of some kind that has this switch inside it and accepts input, but this adds other complexity.
1

I don't know if that way is possible, but you can instance dynamic component with ngComponentOutlet directive. You pass the active component in directive.

In HTML:

<ng-container *ngComponentOutlet="activeComponent"></ng-container>

In TS:

import { Component1 } from './../component1';
import { Component2 } from './../component2';


/** ...SOME DECLARATIONS HERE.... **/

get activeComponent() {
 switch(this.currentOption) {
   case 1: return Component1;
   case 2 : return Component2;
 }
}

3 Comments

If you have a switch inside that... for what not use ngswitch or for what not use ngif in the html... it's the same but with ngComponentOutlet more complicated and unnecessary
But *ngSwitch and *ngIf you have to put all components in HTML, and I think he didn't want that way.
I know...but It's not what he wants, it's what is well done

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.