1

I think I have a knot in my brain, it can't be that difficult.

I have an Angular component and it is called in my app.html <app-custom></app-custom> and if app-custom does not exist, a default component should be called instead <app-default></app-default>

I don't know how to check if the app-custom component exits or not.

4
  • you'd probably get an exception at build-time. You should probably move that logic into the component itself. What's the use-case? Commented Jul 9 at 16:42
  • what does the piece "if app-custom does not exist" mean? Is that not a component in your application code? I think if you expand this one the community could help you Commented Jul 9 at 17:48
  • Its a standard for many projects and in some this component exists, in other not. So i want to test if it exists and than call it other wise call an always existing default component. So we not always have to change it Commented Jul 10 at 3:40
  • @browsermator no if it not exists and i call it i don't get an error, i get a blank page Commented Jul 10 at 3:41

1 Answer 1

1

Option 1:

We can add the default component inside the custom component, the default will be displayed, if the custom component does not exist. But you need to ensure there is no ng-content inside the custom component, else the default component will render inside the custom component.

index.html

<app-custom>
  <app-default></app-default>
</app-custom>

Option 2:

Create a root component app-root which always exists.

Then use @defer and @error to create the fallback, we need to add this to the app-root component.

@defer {
  <app-custom></app-custom>
} @error {
  <app-default></app-default>
}

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

1 Comment

ah, I knew it had to be really easy, but sometimes you just make stupid mistakes. Thank you! Option 2 doesn't work for me somehow, but option 1 works perfectly

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.