4

Hi I have a webpage with multiple layouts that I want to switch to. Similar to codepen.io. What is the best way to do it in angular2?

Say I have 4 layouts. I can put *ngIf="layout1" and put first layout and then *ngIf="layout2" and put the second layout, and so forth. But is it the best way? Note that when changing the layout, both html and css changes.

1 Answer 1

1

There is also ngSwitch which is convenient when there are more than one or two options. This way you have everything in one component. I think it's a good option if it is probable that switching between layouts is common and done often within one user session.

If this is mostly a one-off decision (for example based on the screen dimenions) one of the following options might be a better fit.

You can also use DynamicComponentLoader. Each layout is packed in a component and you add the component that fits the current selection.
See also http://www.syntaxsuccess.com/viewarticle/loading-components-dynamically-in-angular-2.0

Another way is using the router to add a component depending on the Url or Url parameters.
See also https://angular.io/docs/ts/latest/guide/router.html

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.