1

I need to figure out how I can have different views(html+css) for a component. A lot of people say that it's better to have multiple components for each for each of those views and then use a service to interact but my case is as follow: I have a controller with a view that is basically a layout. Say my layout has 3 panes on top and one pane in the bottom. Now I have button in my view to change the layout to two panes on top and two panes on the bottom. So basically my data does not change. Its just a change in the html and css. also if the first layout is filled with some data I dont want to change it or reinitialize it when changing the layout since the change is only a change on layout not the data.

I have difficulty figuring out how I can achieve this in angular2. Any ideas?

2 Answers 2

2

so you want to add html and css or just change the actual template?

If you just want to change the actual html , i personally suggest that you use states instead of different views. And based on the states move the html around. I had the same issue myself and i solved it by rethinking the layout and ended in finding a simpler layout structure.

Hope this helps. Enjoy coding.

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

Comments

0

You can have two views in one template and switch between them by setting a flat:

<div *ngIf="firstLayout">
  <!-- first layout -->
</div>
<div *ngIf="!firstLayout">
  <!-- first layout -->
</div>

2 Comments

I have thought about this as well. The question is is this the most performant way? I was thinking maybe there is a way(sort of modular) where I can load a specific view based on my input layout. Otherwise It will be a big file with a lot of redundancy since most of the elements in these views are the same.
You can use DynamicComponentLoader or the router as well but performance mostly depends on the current use case. Router supports lazy loading of components which might be more efficient if usually only one option is used by one client. If the user switches often during one session *ngIf might be more efficient.

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.