Aditya Ponkshe
Aditya Ponkshe

Reputation: 3890

Advantages of using `ng-include src`

I am working on an application which contains number of HTML and JS files.

In two different HTML files, one div structure (which uses ng-repeat) is going to be same. Controller which will render this div structure is also going to be the same.

Style of this div structure is going to be different on both HTML pages.

Now the question is , should I create a different HTML file which will have this div structure in it and then use ng-class to give different style to this structure. This generic HTML will be included using ng-include src.

or should I just copy-paste same div structure in two different HTML files? I feel advantage of doing this is that I don't have to maintain number of flags and use complex ng-class structure.

So to sum it all up, what are the advantages of including one HTML file using ng-include src over copying div structure in two different HTML files.

Upvotes: 1

Views: 541

Answers (1)

Jonathan Wilson
Jonathan Wilson

Reputation: 4305

The advantage of ng-include over copy and paste here, as you said, is that of maintenance. When it comes time to change the HTML content for some reason, you will not have to make changes in two (or more) places; just one. So, if the HTML fragments really are identical and will change together as your application grows, use ng-include.

To style the two fragments differently: yes, you can ng-class to attach different css classes based on some basic logic. As for the "complete ng-class structure" you mentioned, maybe there is a way for you to simplify that. Show some more code and I can help you make it simpler.

And if at some point of time in future, the styles and of other attributes of this deviates, don't hesitate to fork the template and/or controller. Maintainability of each solution discussed in this example is dependent on how much commonality exists, which can change over time.

Upvotes: 1

Related Questions