Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 21e74c2

Browse files
committed
fix(ngView): controller not published
corrected omitted assignment of controller to the element data object. Without this fix the controller created by ngView is not accessible from the browser debugger.
1 parent 6c5a05a commit 21e74c2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/directive/ngView.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
149149

150150
lastScope = current.scope = scope.$new();
151151
if (current.controller) {
152-
$controller(current.controller, {$scope: lastScope});
152+
element.contents().
153+
data('$ngControllerController', $controller(current.controller, {$scope: lastScope}));
153154
}
154155

155156
link(lastScope);

test/directive/ngViewSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,33 @@ describe('ng-view', function() {
408408
expect($rootScope.load).toHaveBeenCalledOnce();
409409
});
410410
})
411+
412+
413+
it('should set $scope and $controllerController on the view', function() {
414+
function MyCtrl($scope) {
415+
$scope.state = 'WORKS';
416+
$scope.ctrl = this;
417+
}
418+
419+
module(function($routeProvider) {
420+
$routeProvider.when('/foo', {template: 'tpl.html', controller: MyCtrl});
421+
});
422+
423+
inject(function($templateCache, $location, $rootScope, $route) {
424+
$templateCache.put('tpl.html', [200, '<div>{{state}}</div>', {}]);
425+
426+
$location.url('/foo');
427+
$rootScope.$digest();
428+
expect(element.text()).toEqual('WORKS');
429+
430+
var div = element.find('div');
431+
expect(nodeName_(div.parent())).toEqual('NG:VIEW');
432+
433+
expect(div.scope()).toBe($route.current.scope);
434+
expect(div.scope().hasOwnProperty('state')).toBe(true);
435+
expect(div.scope().state).toEqual('WORKS');
436+
437+
expect(div.controller()).toBe($route.current.scope.ctrl);
438+
});
439+
});
411440
});

0 commit comments

Comments
 (0)