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

Commit 5d09a1e

Browse files
committed
fix(ng-view, ng-include): onload and $contentLoaded
- change custom onload directive to special arguments recongnized by both ng-view and ng-include - rename $contentLoaded event to $viewContentLoaded and $includeContentLoaded - add event docs
1 parent f54db2c commit 5d09a1e

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

src/AngularPublic.js

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function publishExternalAPI(angular){
6969
script: scriptDirective,
7070
select: selectDirective,
7171
style: styleDirective,
72-
onload: onloadDirective,
7372
option: optionDirective,
7473
ngBind: ngBindDirective,
7574
ngBindHtml: ngBindHtmlDirective,

src/directive/ngInclude.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,24 @@
6363
</doc:scenario>
6464
</doc:example>
6565
*/
66+
67+
68+
/**
69+
* @ngdoc event
70+
* @name angular.module.ng.$compileProvider.directive.ng:include#$includeContentLoaded
71+
* @eventOf angular.module.ng.$compileProvider.directive.ng:include
72+
* @eventType emit on the current ng:include scope
73+
* @description
74+
* Emitted every time the ng:include content is reloaded.
75+
*/
6676
var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile',
6777
function($http, $templateCache, $anchorScroll, $compile) {
6878
return {
6979
restrict: 'EA',
7080
compile: function(element, attr) {
7181
var srcExp = attr.src,
7282
scopeExp = attr.scope || '',
83+
onloadExp = attr.onload || '',
7384
autoScrollExp = attr.autoscroll;
7485

7586
return function(scope, element, attr) {
@@ -106,7 +117,8 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
106117
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
107118
$anchorScroll();
108119
}
109-
scope.$emit('$contentLoaded');
120+
scope.$emit('$includeContentLoaded');
121+
scope.$eval(onloadExp);
110122
}
111123
}).error(clearContent);
112124
} else {

src/directive/ngView.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,27 @@
9393
</doc:scenario>
9494
</doc:example>
9595
*/
96+
97+
98+
/**
99+
* @ngdoc event
100+
* @name angular.module.ng.$compileProvider.directive.ng:view#$viewContentLoaded
101+
* @eventOf angular.module.ng.$compileProvider.directive.ng:view
102+
* @eventType emit on the current ng:view scope
103+
* @description
104+
* Emitted every time the ng:view content is reloaded.
105+
*/
96106
var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$compile',
97107
'$controller',
98108
function($http, $templateCache, $route, $anchorScroll, $compile,
99109
$controller) {
100110
return {
101111
restrict: 'ECA',
102112
terminal: true,
103-
link: function(scope, element) {
113+
link: function(scope, element, attr) {
104114
var changeCounter = 0,
105-
lastScope;
115+
lastScope,
116+
onloadExp = attr.onload || '';
106117

107118
scope.$on('$afterRouteChange', function(event, next, previous) {
108119
changeCounter++;
@@ -142,7 +153,8 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
142153
}
143154

144155
link(lastScope);
145-
lastScope.$emit('$contentLoaded');
156+
lastScope.$emit('$viewContentLoaded');
157+
lastScope.$eval(onloadExp);
146158

147159
// $anchorScroll might listen on event...
148160
$anchorScroll();
@@ -155,15 +167,3 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
155167
}
156168
};
157169
}];
158-
159-
160-
var onloadDirective = valueFn({
161-
restrict: 'AC',
162-
link: function(scope, elm, attr) {
163-
var onloadExp = attr.onload || ''; //workaround for jquery bug #7537)
164-
165-
scope.$on('$contentLoaded', function(event) {
166-
scope.$eval(onloadExp);
167-
});
168-
}
169-
});

test/directive/ngIncludeSpec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ describe('ng:include', function() {
6464
}));
6565

6666

67-
it('should fire $contentLoaded event after linking the content', inject(
67+
it('should fire $includeContentLoaded event after linking the content', inject(
6868
function($rootScope, $compile, $templateCache) {
6969
var contentLoadedSpy = jasmine.createSpy('content loaded').andCallFake(function() {
7070
expect(element.text()).toBe('partial content');
7171
});
7272

7373
$templateCache.put('url', [200, 'partial content', {}]);
74-
$rootScope.$on('$contentLoaded', contentLoadedSpy);
74+
$rootScope.$on('$includeContentLoaded', contentLoadedSpy);
7575

7676
element = $compile('<ng:include src="\'url\'"></ng:include>')($rootScope);
7777
$rootScope.$digest();
@@ -193,8 +193,8 @@ describe('ng:include', function() {
193193
it('should compile only the content', inject(function($compile, $rootScope, $templateCache) {
194194
// regression
195195

196-
var onload = jasmine.createSpy('$contentLoaded');
197-
$rootScope.$on('$contentLoaded', onload);
196+
var onload = jasmine.createSpy('$includeContentLoaded');
197+
$rootScope.$on('$includeContentLoaded', onload);
198198
$templateCache.put('tpl.html', [200, 'partial {{tpl}}', {}]);
199199

200200
element = $compile('<div><div ng:repeat="i in [1]">' +

test/directive/ngViewSpec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ describe('ng:view', function() {
271271
inject(function($templateCache, $rootScope, $location) {
272272
$rootScope.$on('$beforeRouteChange', logger('$beforeRouteChange'));
273273
$rootScope.$on('$afterRouteChange', logger('$afterRouteChange'));
274-
$rootScope.$on('$contentLoaded', logger('$contentLoaded'));
274+
$rootScope.$on('$viewContentLoaded', logger('$viewContentLoaded'));
275275

276276
$templateCache.put('tpl.html', [200, '{{value}}', {}]);
277277
$location.path('/foo');
278278
$rootScope.$digest();
279279

280280
expect(element.text()).toBe('bound-value');
281281
expect(log).toEqual(['$beforeRouteChange', '$afterRouteChange', 'init-ctrl',
282-
'$contentLoaded']);
282+
'$viewContentLoaded']);
283283
});
284284
});
285285

0 commit comments

Comments
 (0)