0

I'm using the same portion of html code in different parts of a page and I'd like to avoid duplicating it.

The only things that change are a few ones that can be parametrised.

Reading here, I saw one option could be to use a directive: http://fdietz.github.io/recipes-with-angular-js/directives/rendering-an-html-snippet-in-a-directive.html

Would this be the best approach?

The snippet of code I'd like to use is not very short.

I at first tried using ng-include with ng-init to set the parameters but this wasn't a good idea since I was changes them in the scope and so they where changing everywhere. (I saw that this works inside of a ng-repeat because then you have a different scope for it)

Thanks.

Giovanni
  • 11
  • 1
  • 4
  • ng-include is a way to reuse partial views. You need to provide us an example as why it doesn't fit your requirements. – Gorgi Rankovski Jun 13 '14 at 08:27
  • He already did, he's changing scope variables inside the snippet and with ng-include those end up being shared between all the snippets. – ivarni Jun 13 '14 at 10:07
  • @Giovanni A directive sounds like a good match for your usecase, it doesn't really matter that the snippet is short. But please read the FAQ on this site before asking non-specific questions. – ivarni Jun 13 '14 at 10:08
  • This guy might have your solution: http://stackoverflow.com/a/20639139/632604 – Gorgi Rankovski Jun 14 '14 at 08:36
  • @ivarni thanks for your answer; I tried to make it more specific as possible. Trying to go further I'd then ask if that can be done using the controller (having the partial view loaded by the controller) or nesting another controller into the main one to have a different scope when using ng-include – Giovanni Jun 14 '14 at 15:07
  • @GorgiRankovski you mean the one from John Culviner, right? Also the github link provided there looks interesting: https://github.com/angular/angular.js/pull/1227 Thanks. – Giovanni Jun 14 '14 at 15:12

0 Answers0