diff --git a/src/ng1/directives/viewDirective.ts b/src/ng1/directives/viewDirective.ts index 66f59a2a7..9d98af550 100644 --- a/src/ng1/directives/viewDirective.ts +++ b/src/ng1/directives/viewDirective.ts @@ -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 || { viewDecl: {} }; $element.html(cfg.template || initial); diff --git a/test/viewDirectiveSpec.js b/test/viewDirectiveSpec.js index d896fb3ea..8fa1bf036 100644 --- a/test/viewDirectiveSpec.js +++ b/test/viewDirectiveSpec.js @@ -88,7 +88,7 @@ describe('uiView', function () { controller: function() { this.someProperty = "value" }, - template: "hi", + template: "{{vm.someProperty}}", controllerAs: "vm" }, lState = { @@ -324,7 +324,7 @@ describe('uiView', function () { }); it('should instantiate a controller with controllerAs', inject(function($state, $q) { - elem.append($compile('
{{vm.someProperty}}
')(scope)); + elem.append($compile('
')(scope)); $state.transitionTo(kState); $q.flush(); @@ -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: '' }) + .state('a.b', { template: 'anything' }); + })); + + beforeEach(inject(function ($rootScope, _$compile_) { + scope = $rootScope.$new(); + $compile = _$compile_; + elem = angular.element('
'); + })); + + 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('
')(scope)); + $state.transitionTo('a.b'); + $q.flush(); + expect(aliveCount).toBe(0); + })); +}); + describe('uiView controllers or onEnter handlers', function() { var el, template, scope, document, count;