Skip to content

fix(uiView): do not leave initial view scope undestroyed #3164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2016
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
7 changes: 6 additions & 1 deletion src/ng1/directives/viewDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,15 @@ function $ViewDirectiveFill ($compile: ICompileService, $controller: IController
priority: -400,
compile: function (tElement: JQuery) {
let initial = tElement.html();
tElement.empty();

return function (scope: IScope, $element: JQuery) {
let data: UIViewData = $element.data('$uiView');
if (!data) return;
if (!data) {
$element.html(initial);
$compile($element.contents())(scope);
return;
}

let cfg: Ng1ViewConfig = data.$cfg || <any> { viewDecl: {} };
$element.html(cfg.template || initial);
Expand Down
52 changes: 50 additions & 2 deletions test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('uiView', function () {
controller: function() {
this.someProperty = "value"
},
template: "hi",
template: "{{vm.someProperty}}",
controllerAs: "vm"
},
lState = {
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('uiView', function () {
});

it('should instantiate a controller with controllerAs', inject(function($state, $q) {
elem.append($compile('<div><ui-view>{{vm.someProperty}}</ui-view></div>')(scope));
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test broke but not because of what it tests. The template property was not originally in the test and after this change the template property overwrites the initial view.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this was originally trying to test >_>

$state.transitionTo(kState);
$q.flush();

Expand Down Expand Up @@ -726,6 +726,54 @@ describe("UiView", function() {
}));
});

describe('uiView transclusion', function() {
var scope, $compile, elem;

beforeEach(function() {
app = angular.module('foo', []);

app.directive('scopeObserver', function() {
return {
restrict: 'E',
link: function(scope) {
scope.$emit('directiveCreated');
scope.$on('$destroy', function() {
scope.$emit('directiveDestroyed');
});
}
};
});
});

beforeEach(module('ui.router', 'foo'));

beforeEach(module(function($stateProvider) {
$stateProvider
.state('a', { template: '<ui-view><scope-observer></scope-observer></ui-view>' })
.state('a.b', { template: 'anything' });
}));

beforeEach(inject(function ($rootScope, _$compile_) {
scope = $rootScope.$new();
$compile = _$compile_;
elem = angular.element('<div>');
}));

it('should not link the initial view and leave its scope undestroyed when a subview is activated', inject(function($state, $q) {
var aliveCount = 0;
scope.$on('directiveCreated', function() {
aliveCount++;
});
scope.$on('directiveDestroyed', function() {
aliveCount--;
});
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
$state.transitionTo('a.b');
$q.flush();
expect(aliveCount).toBe(0);
}));
});

describe('uiView controllers or onEnter handlers', function() {
var el, template, scope, document, count;

Expand Down