0

I'm trying to get the ng-repeat directive that is placed into another directive which transcludes its content. The point here is to retrieve the transcluded content before it gets compiled, e.g.:

<tr ng-repeat="item in items">
    <td>{{item}}</td>
</tr>

Here's a simple example of what I am trying to achieve (and a plnkr):

app.directive('pageable', function() {
  return {
    restrict:'E',
    template:'<div ng-transclude></div>',
    transclude: true,
    priority: 1001,
    scope: true,
    compile: function(element, attrs) {
      console.log(element.html());
      console.log(element);
    }
  };
});

Actually, I get either already compiled ng-repeat directive or simply comment like:

<!-- ng-repeat: item in items -->

Is there a way to get the transcluded content before it gets compiled?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • what's the point of this exercise? – Claies Aug 09 '15 at 03:39
  • the point here is to replace ng-repeat with a dir-paginate directive (the directive which provides pagination stuff), as well as do some other things with transcluded content. –  Aug 09 '15 at 03:42
  • ok, so then how would the content be expanded? Is your `dir-paginate` going to somehow expand the `ng-repeat` on it's own? – Claies Aug 09 '15 at 03:43
  • also, this doesn't really make sense within the context of how `ng-repeat` works anyway; the element that the repeat is placed on is the element that gets replicated. are you suggesting that you want a `dir-paginate` element for every `item`? What you *probably* want is to wrap a div around this element, and put your `dir-paginate` on that. – Claies Aug 09 '15 at 03:46
  • not exactly. my outer directive which encloses ng-repeat is going to retrieve the expression from ng-repeat, augument it (add how many values per page are going to be shown and other stuff) and replace ng-repeat altogether. I'm going to achieve this by retrieving the non-compiled ng-repeat directive + some jquery selector magic and angular $compile serivce. –  Aug 09 '15 at 03:50
  • unless you hack the angular source or create your own replacement, this isn't going to work. Even though `ng-repeat` has a priority of `1000`, and your directive has a priority of `1001` should run before it, `ng-transclude` is actually priority `0`, so it will *always* run after `ng-repeat`, even though the logic of your directive runs earlier. – Claies Aug 09 '15 at 03:56
  • there is a good article on all the phases with diagrams, http://stackoverflow.com/questions/24615103/angular-directives-when-and-how-to-use-compile-controller-pre-link-and-post. perhaps you can follow the phases the directive goes through, and find an appropriate place to do your modifications from those diagrams. – Claies Aug 09 '15 at 04:01
  • thanks! I'll have look. by the way, I think I should reconsider the whole concept of my directive. maybe it would be better to start with a right directive from the beginning and not to do all this crazy stuff with a compilation and such. –  Aug 09 '15 at 04:11
  • @maseev, the point of my comment on your previous (almost exact duplicate) question was that you could ask a new and different question about how to change `ng-repeat` to another directive... but you asked exactly the same question (about getting the template of `ng-repeat`) which I already answered here: http://stackoverflow.com/questions/31890510/how-to-get-ng-repeat-directive-template-before-it-gets-compiled – New Dev Aug 09 '15 at 04:14
  • I'm not trying to retrive ng-repeat template. I'm trying to retrive the ng-repeat itself before it gets compiled, so I would be able to get the 'for each' expression and stuff underneath it. –  Aug 09 '15 at 04:19
  • @maseev, I get it, but your questions reads: "The point here is to retrieve the transcluded content before it gets compiled" – New Dev Aug 09 '15 at 04:24
  • yes, that's right. By the transcluded content I mean everything that is underneath the pageable directive. –  Aug 09 '15 at 04:28
  • @maseev, ok... well, to avoid an XY problem, why don't you explain what you ultimate objective is. Why do you need to have `ng-repeat` in the View in the first place, only to have it changed to something else? – New Dev Aug 09 '15 at 05:56
  • look at priority as well... – Callum Linington Sep 29 '15 at 12:30

0 Answers0