Skip to content

Commit c1dc7fc

Browse files
Merge pull request #2475 from hshn/patch-1
fix(viewDirective): fix $element injection for controller
2 parents 7174ee5 + c5249bb commit c1dc7fc

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/ng1/viewDirective.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function $ViewDirectiveFill ( $compile, $controller, $interpolate, $injec
289289

290290
if (controller) {
291291
let locals = data.$locals;
292-
let controllerInstance = $controller(controller, extend(locals, { $scope: scope }));
292+
let controllerInstance = $controller(controller, extend(locals, { $scope: scope, $element: $element }));
293293
if (controllerAs) scope[controllerAs] = controllerInstance;
294294
$element.data('$ngControllerController', controllerInstance);
295295
$element.children().data('$ngControllerController', controllerInstance);

test/viewDirectiveSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ describe('uiView', function () {
9797
template: 'view3'
9898
}
9999
}
100+
},
101+
mState = {
102+
template: 'mState',
103+
controller: function ($scope, $element) {
104+
$scope.elementId = $element.attr('id');
105+
}
100106
};
101107

102108
beforeEach(module(function ($stateProvider) {
@@ -113,6 +119,7 @@ describe('uiView', function () {
113119
.state('j', jState)
114120
.state('k', kState)
115121
.state('l', lState)
122+
.state('m', mState)
116123
}));
117124

118125
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -307,6 +314,14 @@ describe('uiView', function () {
307314
expect(elem.text()).toBe('value');
308315
}));
309316

317+
it('should instantiate a controller with both $scope and $element injections', inject(function ($state, $q) {
318+
elem.append($compile('<div><ui-view id="mState">{{elementId}}</ui-view></div>')(scope));
319+
$state.transitionTo(mState);
320+
$q.flush();
321+
322+
expect(elem.text()).toBe('mState');
323+
}));
324+
310325
describe('play nicely with other directives', function() {
311326
// related to issue #857
312327
it('should work with ngIf', inject(function ($state, $q, $compile) {

0 commit comments

Comments
 (0)