-
Notifications
You must be signed in to change notification settings - Fork 27.5k
docs($compile): add double compilation known issue #15392
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also put that in the compiler guide (with a short note here in the API docs that double-compilation is not supported).
* elements. | ||
* | ||
* This can cause unpredictable behavior, e.g. `ngModel` $formatters and $parsers will be | ||
* attached again to the ngModelController. It can also degrade performance, as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngModel
$formatters and $parsers will be attached again to the ngModelController
Why? ngModel
seems to only be compiled once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I was actually thinking of an issue with replace. I will change the example.
* attached again to the ngModelController. It can also degrade performance, as | ||
* watchers for text interpolation are added twice to the scope. | ||
* | ||
* Double compilation should therefore avoided. In the above example, the better way is to only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoided --> be avoided
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the better way --> a better way (?)
link: function(scope, element, attrs) { | ||
var newEl = angular.element('<input ng-model="$ctrl.value">'); | ||
$compile(newEl)(scope); // Only compile the new element | ||
element.append(newEl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think usually it is preferrable to first append the element and then compile it, in order to have proper context (e.g. requiring parent directive controllers).
angular.module('app').directive('addOptions', function($compile) { | ||
return { | ||
link: function(scope, element, attrs) { | ||
attrs.$set('addInput', null) // To stop infinite compile loop |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addInput --> addOptions
* | ||
* In that case, it is necessary to intercept the *initial* compilation of the element: | ||
* | ||
* 1. give your directive the `terminal` property and a higher priority than directives |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capitalize these sentences.
* that should not be compiled twice. In the example, the compiler will only compile directives | ||
* which have a priority of 100 or higher. | ||
* 2. inside this directive's compile function, remove the original directive attribute from the element, | ||
* and add any other directive attributes. Removing the attribute is necessary, because otherwise the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to remove the attribute, since you pass the priority to $compile
.
I've updated the example and moved it to the compiler guide, and added a short paragraph with a link to the compile api docs |
a622fa7
to
af8f311
Compare
return { | ||
link: function(scope, element, attrs) { | ||
var newEl = angular.element('<span ng-show="showHint"> My Hint</span>'); | ||
element.on('mouseenter mouseout', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The opposite of mouseenter
is mouseout
mouseleave
😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand. Do you mean it should be mouseleave
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry, that's what I meant 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still mouseout
😞
to a button with `ngClick` on it: | ||
|
||
``` | ||
angular.module('app').directive('addMouseover', function($compile) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a big, fat comment or a danger alert that this is what you should NOT do. People tend to get confused when the docs show bad examples to be avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but we should have a general solution to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried putting a div around it, but then the code blocks aren't rendered correctly. Seems to be an issue with the marked library: markedjs/marked#596
scope.$apply('showHint = !showHint'); | ||
}); | ||
|
||
attrs.$set('addMouseover', null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to remove this any more.
``` | ||
|
||
Another scenario is adding a directive programmatically to a compiled element and then executing | ||
compile again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, lets make it clear this is a BAD thing.
which have a priority of 100 or higher. | ||
2. Inside this directive's compile function, remove the original directive attribute from the element, | ||
and add any other directive attributes. Removing the attribute is necessary, because otherwise the | ||
compilation would result in an infinite loop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to remove the attribute, since you pass the priority to $compile.
return { | ||
priority: 100, // ngModel has priority 1 | ||
terminal: true, | ||
template: '<input ng-model="$ctrl.value">', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't want to have a template.
9bce926
to
f97ed1f
Compare
f97ed1f
to
53bef98
Compare
I've updated the PR @gkalpak PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of typos.
LGTM otherwise 👍
return { | ||
link: function(scope, element, attrs) { | ||
var newEl = angular.element('<span ng-show="showHint"> My Hint</span>'); | ||
element.on('mouseenter mouseout', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still mouseout
😞
2. Inside this directive's compile function, add any other directive attributes to the template. | ||
3. Compile the element, but restrict the maximum priority, so that any already compiled directives | ||
(including the `addOptions` directive) are not compiled again. | ||
4. In the link function, link the compiled element with the element's scope |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing period at the end.
function on the directive element. In the following **faulty example**, a directive adds a mouseover behavior | ||
to a button with `ngClick` on it: | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be ```js
(here and below) or does marked autodetect JS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always thought it auto-detects
to a button with `ngClick` on it: | ||
|
||
``` | ||
angular.module('app').directive('addMouseover', function($compile) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still prefer a comment above this line; something like:
// BAD - Don't do this
And an equivalent comment in the recommended alternatives; e.g.:
// OK - You can do this
But I can leave without if you think it's too much. Up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave it as is, and we'll see how soon someone stumbles over this
I think the part about |
There were two occurrences of |
Omg ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (as soon as travis is happy)
No description provided.