1

If I write twice the same directive as attribute, I get twice the result, but when I write it twice as element, I only get the result once, why?

I have a very simple directive:

.directive("ngMyText", function(){
    return {
        restrict: 'AE'
      };
})

An $scope with a collection of items:

$scope.items = [
    { text:"AAA", show:true },
    { text:"BBB", show:true }
];

Therefore, when doing this:

<div ng-controller="myController">
    <div class="container">
        <div data-ng-my-text ng-repeat="item in items | filter:{show:true}" ng-bind="item.text"></div>
        <div data-ng-my-text ng-repeat="item in items | filter:{show:true}" ng-bind="item.text"></div>
    </div>

    <div class="container">
        <ng-my-text ng-repeat="item in items | filter:{show:true}" ng-bind="item.text" />
        <ng-my-text ng-repeat="item in items | filter:{show:true}" ng-bind="item.text" />
    </div>
</div>

I would expect to get the collection rendered twice in each container, but in the second container only happens once. Why does this happen?

I have created an runable example with the problem: http://jsfiddle.net/vtortola/mzAPk/

Cheers.

vtortola
  • 34,709
  • 29
  • 161
  • 263

1 Answers1

2

See Are (non-void) self-closing tags valid in HTML5? This

<ng-my-text ng-repeat="item in items | filter:{show:true}" 
ng-bind="item.text" />

should be

<ng-my-text ng-repeat="item in items | filter:{show:true}" 
ng-bind="item.text"></ng-my-text>
Community
  • 1
  • 1
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53