@@ -386,7 +386,7 @@ Double compilation occurs when an already compiled part of the DOM gets compiled
386
386
undesired effect and can lead to misbehaving directives, performance issues, and memory
387
387
leaks.
388
388
A common scenario where this happens is a directive that calls `$compile` in a directive link
389
- function on the directive element. In the following example, a directive adds a mouseover behavior
389
+ function on the directive element. In the following **faulty example** , a directive adds a mouseover behavior
390
390
to a button with `ngClick` on it:
391
391
392
392
```
@@ -423,11 +423,10 @@ angular.module('app').directive('addMouseover', function($compile) {
423
423
return {
424
424
link: function(scope, element, attrs) {
425
425
var newEl = angular.element('<span ng-show="showHint"> My Hint</span>');
426
- element.on('mouseenter mouseout ', function() {
426
+ element.on('mouseenter mouseleave ', function() {
427
427
scope.$apply('showHint = !showHint');
428
428
});
429
429
430
- attrs.$set('addMouseover', null);
431
430
element.append(newEl);
432
431
$compile(newEl)(scope); // Only compile the new element
433
432
}
@@ -436,7 +435,7 @@ angular.module('app').directive('addMouseover', function($compile) {
436
435
```
437
436
438
437
Another scenario is adding a directive programmatically to a compiled element and then executing
439
- compile again.
438
+ compile again. See the following **faulty example**:
440
439
441
440
```html
442
441
<input ng-model="$ctrl.value" add-options>
@@ -459,19 +458,16 @@ In that case, it is necessary to intercept the *initial* compilation of the elem
459
458
1. Give your directive the `terminal` property and a higher priority than directives
460
459
that should not be compiled twice. In the example, the compiler will only compile directives
461
460
which have a priority of 100 or higher.
462
- 2. Inside this directive's compile function, remove the original directive attribute from the element,
463
- and add any other directive attributes. Removing the attribute is necessary, because otherwise the
464
- compilation would result in an infinite loop.
465
- 3. Compile the element but restrict the maximum priority, so that any already compiled directives
466
- are not compiled twice.
461
+ 2. Inside this directive's compile function, add any other directive attributes to the template.
462
+ 3. Compile the element, but restrict the maximum priority, so that any already compiled directives
463
+ (including the `addOptions` directive) are not compiled again.
467
464
4. In the link function, link the compiled element with the element's scope
468
465
469
466
```
470
467
angular.module('app').directive('addOptions', function($compile) {
471
468
return {
472
469
priority: 100, // ngModel has priority 1
473
470
terminal: true,
474
- template: '<input ng-model="$ctrl.value">',
475
471
compile: function(templateElement, templateAttributes) {
476
472
templateAttributes.$set('ngModelOptions', '{debounce: 1000}');
477
473
0 commit comments