Skip to content

Commit a130bb8

Browse files
committed
add onload attribute to ng:include
1 parent cc74976 commit a130bb8

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/widgets.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ angularWidget('option', function(){
494494
*
495495
* @param {string} src expression evaluating to URL.
496496
* @param {Scope=} [scope=new_child_scope] expression evaluating to angular.scope
497+
* @param {string=} onload Expression to evaluate when a new partial is loaded.
497498
*
498499
* @example
499500
* <select name="url">
@@ -521,7 +522,8 @@ angularWidget('option', function(){
521522
angularWidget('ng:include', function(element){
522523
var compiler = this,
523524
srcExp = element.attr("src"),
524-
scopeExp = element.attr("scope") || '';
525+
scopeExp = element.attr("scope") || '',
526+
onloadExp = element[0].getAttribute('onload') || ''; //workaround for jquery bug #7537
525527
if (element[0]['ng:compiled']) {
526528
this.descend(true);
527529
this.directives(true);
@@ -546,13 +548,15 @@ angularWidget('ng:include', function(element){
546548
});
547549
this.$watch(function(){return changeCounter;}, function(){
548550
var src = this.$eval(srcExp),
549-
useScope = this.$eval(scopeExp);
551+
useScope = this.$eval(scopeExp);
552+
550553
if (src) {
551554
xhr('GET', src, function(code, response){
552555
element.html(response);
553556
childScope = useScope || createScope(scope);
554557
compiler.compile(element)(element, childScope);
555558
childScope.$init();
559+
scope.$eval(onloadExp);
556560
});
557561
} else {
558562
childScope = null;

test/widgetsSpec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,19 @@ describe("widget", function(){
532532
// we need to have real events on the scopes.
533533
expect(element.text()).toEqual('4');
534534
});
535+
536+
it('should evaluate onload expression when a partial is loaded', function() {
537+
var element = jqLite('<ng:include src="url" onload="loaded = true"></ng:include>');
538+
var scope = angular.compile(element);
539+
540+
expect(scope.loaded).not.toBeDefined();
541+
542+
scope.url = 'myUrl';
543+
scope.$inject('$xhr.cache').data.myUrl = {value:'my partial'};
544+
scope.$init();
545+
expect(element.text()).toEqual('my partial');
546+
expect(scope.loaded).toBe(true);
547+
});
535548
});
536549

537550
describe('a', function() {

0 commit comments

Comments
 (0)