Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f38010d

Browse files
committed
fix(compiler): revert 8611ebe - calling \$digest after linking
Change introduced by me in 8611ebe results in considerable inefficiencies when the compiler and linker is used from within a widget, in which case, we call $digest unnecessary since it will be called by the $apply which called the directive/widget in the first place. There are only two places when the extra $digest call can be useful - when manually bootstrapping the app or in tests. However even in tests this behavior can result in unwanted results (especially when ng:controller is involved). So it is better to leave it for the developer to call $digest when it is really needed.
1 parent 7fc18b2 commit f38010d

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ explicitly.
1717
<script src="http://code.angularjs.org/angular.js"></script>
1818
<script>
1919
angular.element(document).ready(function() {
20-
angular.compile(document)();
20+
angular.compile(document)().$apply();
2121
});
2222
</script>
2323
</head>

src/Compiler.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ Template.prototype = {
119119
* the same scope as the one passed into the template function, or if none were provided it's the
120120
* newly create scope.
121121
*
122+
* It is important to understand that the returned scope is "linked" to the view DOM, but no linking
123+
* (instance) functions registered by {@link angular.directive directives} or
124+
* {@link angular.widget widgets} found in the template have been executed yet. This means that the
125+
* view is likely empty and doesn't contain any values that result from evaluation on the scope. To
126+
* bring the view to life, the scope needs to run through a $digest phase which typically is done by
127+
* Angular automatically, except for the case when an application is being
128+
* {@link guide/dev_guide.bootstrap.manual_bootstrap} manually bootstrapped, in which case the
129+
* $digest phase must be invoked by calling {@link angular.scope.$apply}.
130+
*
122131
* If you need access to the bound view, there are two ways to do it:
123132
*
124133
* - If you are not asking the linking function to clone the template, create the DOM element(s)
@@ -209,7 +218,6 @@ Compiler.prototype = {
209218
scope.$element = element;
210219
(cloneConnectFn||noop)(element, scope);
211220
template.link(element, scope);
212-
if (!scope.$$phase) scope.$digest();
213221
return scope;
214222
};
215223
},

test/CompilerSpec.js

-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ describe('compiler', function() {
110110
expect(sortedHtml(scope.$element)).
111111
toEqual('<div>' +
112112
'before<#comment></#comment>' +
113-
'<span>x</span>' +
114113
'after' +
115114
'</div>');
116115
scope.value = 1;
@@ -119,7 +118,6 @@ describe('compiler', function() {
119118
toEqual('<div>' +
120119
'before<#comment></#comment>' +
121120
'<span>x</span>' +
122-
'<span>x</span>' +
123121
'after' +
124122
'</div>');
125123
scope.value = 2;
@@ -129,7 +127,6 @@ describe('compiler', function() {
129127
'before<#comment></#comment>' +
130128
'<span>x</span>' +
131129
'<span>x</span>' +
132-
'<span>x</span>' +
133130
'after' +
134131
'</div>');
135132
scope.value = 3;
@@ -140,7 +137,6 @@ describe('compiler', function() {
140137
'<span>x</span>' +
141138
'<span>x</span>' +
142139
'<span>x</span>' +
143-
'<span>x</span>' +
144140
'after' +
145141
'</div>');
146142
});

test/markupSpec.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,12 @@ describe("markups", function() {
167167
});
168168

169169
it('should bind Text with no Bindings', function() {
170-
forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) {
170+
forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected', 'src', 'href'],
171+
function(name) {
171172
compile('<div ng:' + name +'="some"></div>');
172-
expect(element.attr('ng:bind-attr')).toBe('{"' + name +'":"some"}');
173-
expect(element.attr(name)).toBe(name);
173+
expect(sortedHtml(element)).toEqual('<div ng:bind-attr="{"' + name +'":"some"}"></div>');
174174
dealoc(element);
175175
});
176-
177-
compile('<div ng:src="some"></div>');
178-
expect(sortedHtml(element)).toEqual('<div ng:bind-attr="{"src":"some"}" src="some"></div>');
179-
dealoc(element);
180-
181-
compile('<div ng:href="some"></div>');
182-
expect(sortedHtml(element)).toEqual('<div href="some" ng:bind-attr="{"href":"some"}"></div>');
183-
dealoc(element);
184176
});
185177

186178
it('should Parse Text With No Bindings', function() {

0 commit comments

Comments
 (0)