From cd38cbf975b501d846e6149d1d993972a1af0053 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 6 Jun 2012 15:54:40 -0700 Subject: feat(controller): support as instance syntax Support ng-controller="MyController as my" syntax which publishes the controller instance to the current scope. Also supports exporting a controller defined with route: ````javascript angular.module('routes', [], function($routeProvider) { $routeProvider.when('/home', {controller: 'Ctrl as home', templateUrl: '...'}); }); ```` --- test/ng/controllerSpec.js | 11 +++++++++++ test/ng/directive/ngControllerSpec.js | 23 +++++++++++++++++++++++ test/ng/directive/ngViewSpec.js | 21 +++++++++++++++++++++ 3 files changed, 55 insertions(+) (limited to 'test/ng') diff --git a/test/ng/controllerSpec.js b/test/ng/controllerSpec.js index 2abcace9..e34463b0 100644 --- a/test/ng/controllerSpec.js +++ b/test/ng/controllerSpec.js @@ -88,4 +88,15 @@ describe('$controller', function() { expect(ctrl.$scope).toBe(scope); }); + + + it('should publish controller instance into scope', function() { + var scope = {}; + + $controllerProvider.register('FooCtrl', function() { this.mark = 'foo'; }); + + var foo = $controller('FooCtrl as foo', {$scope: scope}); + expect(scope.foo).toBe(foo); + expect(scope.foo.mark).toBe('foo'); + }); }); diff --git a/test/ng/directive/ngControllerSpec.js b/test/ng/directive/ngControllerSpec.js index ab85c569..402ddf09 100644 --- a/test/ng/directive/ngControllerSpec.js +++ b/test/ng/directive/ngControllerSpec.js @@ -3,6 +3,11 @@ describe('ngController', function() { var element; + beforeEach(module(function($controllerProvider) { + $controllerProvider.register('PublicModule', function() { + this.mark = 'works'; + }); + })); beforeEach(inject(function($window) { $window.Greeter = function($scope) { // private stuff (not exported to scope) @@ -27,6 +32,10 @@ describe('ngController', function() { $window.Child = function($scope) { $scope.name = 'Adam'; }; + + $window.Public = function() { + this.mark = 'works'; + } })); afterEach(function() { @@ -41,6 +50,20 @@ describe('ngController', function() { })); + it('should publish controller into scope', inject(function($compile, $rootScope) { + element = $compile('
{{p.mark}}
')($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('works'); + })); + + + it('should publish controller into scope from module', inject(function($compile, $rootScope) { + element = $compile('
{{p.mark}}
')($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('works'); + })); + + it('should allow nested controllers', inject(function($compile, $rootScope) { element = $compile('
{{greet(name)}}
')($rootScope); $rootScope.$digest(); diff --git a/test/ng/directive/ngViewSpec.js b/test/ng/directive/ngViewSpec.js index e9d53110..3150c040 100644 --- a/test/ng/directive/ngViewSpec.js +++ b/test/ng/directive/ngViewSpec.js @@ -55,6 +55,27 @@ describe('ngView', function() { }); + it('should instantiate controller with an alias', function() { + var log = [], controllerScope, + Ctrl = function($scope) { + this.name = 'alias'; + controllerScope = $scope; + }; + + module(function($compileProvider, $routeProvider) { + $routeProvider.when('/some', {templateUrl: '/tpl.html', controller: Ctrl, controllerAlias: 'ctrl'}); + }); + + inject(function($route, $rootScope, $templateCache, $location) { + $templateCache.put('/tpl.html', [200, '
', {}]); + $location.path('/some'); + $rootScope.$digest(); + + expect(controllerScope.ctrl.name).toBe('alias'); + }); + }); + + it('should support string controller declaration', function() { var MyCtrl = jasmine.createSpy('MyCtrl'); -- cgit v1.2.3