Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core/services/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,15 @@ function MdCompilerProvider($compileProvider) {
// Create the specified controller instance.
var ctrl = self._createController(options, injectLocals, locals);

// Registering extra $destroy listeners should be avoided.
// Only register the listener if the controller implements a $onDestroy hook.
if (angular.isFunction(ctrl.$onDestroy)) {
scope.$on('$destroy', function() {
// Call the $onDestroy hook if it's present on the controller.
angular.isFunction(ctrl.$onDestroy) && ctrl.$onDestroy();
});
}

// Unique identifier for AngularJS Route ngView controllers.
element.data('$ngControllerController', ctrl);
element.children().data('$ngControllerController', ctrl);
Expand Down
59 changes: 58 additions & 1 deletion src/core/services/compiler/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('$mdCompiler service', function() {
}
});
});

function compileAndLink(options) {
var compileData;

Expand Down Expand Up @@ -482,4 +482,61 @@ describe('$mdCompiler service', function() {
});
});

describe('AngularJS 1.6+ lifecycle hooks', function() {
var $mdCompiler, pageScope, $rootScope;

beforeEach(module('material.core'));

beforeEach(inject(function($injector) {
$mdCompiler = $injector.get('$mdCompiler');
$rootScope = $injector.get('$rootScope');
pageScope = $rootScope.$new(false);
}));

it('calls $onInit on initialization', function(done) {
var passed = false;

class TestController {
$onInit() { passed = true; }
}

var compileResult = $mdCompiler.compile({
template: '<span></span>',
controller: TestController,
controllerAs: 'vm',
bindToController: true
});

compileResult.then(function(compileOutput) {
compileOutput.link(pageScope).scope();
expect(passed).toBe(true);
done();
});

$rootScope.$apply();
});

it('calls $onDestroy on destruction', function(done) {
var passed = false;

class TestController {
$onDestroy() { passed = true; }
}

var compileResult = $mdCompiler.compile({
template: '<span></span>',
controller: TestController,
controllerAs: 'vm',
bindToController: true
});

compileResult.then(function(compileOutput) {
compileOutput.link(pageScope).scope().$destroy();
expect(passed).toBe(true);
done();
});

$rootScope.$apply();
});
});
});