AngularJS ng-repeat is applied several times in the $ compiled directive

I wrote a directive that dynamically creates a popover for an element:

app.directive('popover', function($compile, $timeout){
    return {    
        link: function(scope, element, attrs) {

            $timeout(function() {

                // grab template
                var tpl = $(element).find('.popover-template')

                // grab popover parts of template
                var template = {
                    //$compile( $(element).siblings(".pop-content").contents() )(scope)
                    title: tpl.find('.template-title').contents(),
                    content: tpl.find('.template-content').contents()
                };

                // render template with angular
                var content = $compile(template.content)(scope);
                var title = $compile(template.title)(scope); 

                $(element).popover({
                    html: true,
                    placement: "right",
                    content: content,
                    title: title
                });

                scope.$digest()
            });

        }

    };
});

In the application, it looks like this:

<span popover>Click me</span>
<div ng-hide="true" class="popover-template">
    <div class="template-title">
        <strong>{{ x.name }} and {{ y.name }}</strong>
    </div>

    <div class="template-content">
        <div>
            <pre>f in [1,2,3]</pre>
            <div ng-repeat="f in [1,2,3]">
                item {{ f }}, index {{ $index }}
            </div>
        </div>
    </div>

</div>

A popover is created and displayed. The name also works correctly. However, ng-repeat is applied several times in any iteration:

enter image description here

As you can see, the iteration, which should only include 3 elements, includes 3 * 3 elements. The directive creates popovers for exactly 3 elements, so I assume that where my mistake is. How can I make sure that inside each popover, it ng-repeatis called only once?

+2
2

popover-template , angular ( ), . ng-repeat :

<!-- original -->
<div ng-repeat="f in [1,2,3]">item {{ f }}, index {{ $index }}</div>

<!-- replaced -->
<div ng-repeat="f in [1,2,3]">item 1, index 0</div>
<div ng-repeat="f in [1,2,3]">item 2, index 1</div>
<div ng-repeat="f in [1,2,3]">item 3, index 2</div>

, 3-n-, 3 , 9.

popover , . $templateCache.

, , HTML .

0

html popover $http templateCache.

HTML:

<span popover>Click me</span>
<script type="text/ng-template" id="popover.html">
  <div class="popover-template">
    <div class="template-title">
      <strong>{{ x.name }} and {{ y.name }}</strong>
    </div>
    <div class="template-content">
      <div>
        <pre>f in [1,2,3] track by $index</pre>
        <div ng-repeat="f in [1,2,3]">
          item {{ f }}, index {{ $index }}
        </div>
      </div>
    </div>
  </div>
</script>

Javascript:

angular.module('app',[]).directive('popover', function($compile, $timeout, $templateCache){
    return {
        link: function(scope, element, attrs) {

            $timeout(function() {
                // grab the template (this is the catch)
                // you can pass the template name as a binding if you want to be loaded dynamically

                var tpl = angular.element($templateCache.get('popover.html'));

                // grab popover parts of template
                var template = {
                    title: tpl.find('.template-title').contents(),
                    content: tpl.find('.template-content').contents()
                };

                // render template with angular
                var content = $compile(template.content)(scope);
                var title = $compile(template.title)(scope); 

                $(element).popover({
                    html: true,
                    placement: "right",
                    content: content,
                    title: title
                });

                scope.$digest()
            });

        }

    };
});

, : http://embed.plnkr.co/IoIG1Y1DT8RO4tQydXnX/

-1

All Articles