mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
refactor($controller): Add $controller service for instantiating controllers
So that we can allow user to override this service and use BC hack: https://gist.github.com/1649788
This commit is contained in:
parent
0196411dbe
commit
dbffbefb7c
7 changed files with 74 additions and 8 deletions
1
angularFiles.js
vendored
1
angularFiles.js
vendored
|
|
@ -13,6 +13,7 @@ angularFiles = {
|
|||
'src/service/browser.js',
|
||||
'src/service/cacheFactory.js',
|
||||
'src/service/compiler.js',
|
||||
'src/service/controller.js',
|
||||
'src/service/cookieStore.js',
|
||||
'src/service/cookies.js',
|
||||
'src/service/defer.js',
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ function publishExternalAPI(angular){
|
|||
$provide.service('$browser', $BrowserProvider);
|
||||
$provide.service('$cacheFactory', $CacheFactoryProvider);
|
||||
$provide.service('$compile', $CompileProvider);
|
||||
$provide.service('$controller', $ControllerProvider);
|
||||
$provide.service('$cookies', $CookiesProvider);
|
||||
$provide.service('$cookieStore', $CookieStoreProvider);
|
||||
$provide.service('$defer', $DeferProvider);
|
||||
|
|
|
|||
|
|
@ -160,12 +160,12 @@ angularDirective("ng:init", function(expression){
|
|||
*/
|
||||
angularDirective("ng:controller", function(expression) {
|
||||
this.scope(true);
|
||||
return ['$injector', '$window', function($injector, $window) {
|
||||
return ['$controller', '$window', function($controller, $window) {
|
||||
var scope = this,
|
||||
Controller = getter(scope, expression, true) || getter($window, expression, true);
|
||||
|
||||
assertArgFn(Controller, expression);
|
||||
$injector.instantiate(Controller, {$scope: scope});
|
||||
$controller(Controller, scope);
|
||||
}];
|
||||
});
|
||||
|
||||
|
|
|
|||
26
src/service/controller.js
Normal file
26
src/service/controller.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
'use strict';
|
||||
|
||||
function $ControllerProvider() {
|
||||
this.$get = ['$injector', function($injector) {
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.module.ng.$controller
|
||||
* @requires $injector
|
||||
*
|
||||
* @param {Function} Class Constructor function of a controller to instantiate.
|
||||
* @param {Object} scope Related scope.
|
||||
* @return {Object} Instance of given controller.
|
||||
*
|
||||
* @description
|
||||
* `$controller` service is responsible for instantiating controllers.
|
||||
*
|
||||
* It's just simple call to {@link angular.module.AUTO.$injector $injector}, but extracted into
|
||||
* a service, so that one can override this service with {@link https://gist.github.com/1649788
|
||||
* BC version}.
|
||||
*/
|
||||
return function(Class, scope) {
|
||||
return $injector.instantiate(Class, {$scope: scope});
|
||||
};
|
||||
}];
|
||||
}
|
||||
|
|
@ -102,8 +102,8 @@
|
|||
|
||||
function $FormFactoryProvider() {
|
||||
var $parse;
|
||||
this.$get = ['$rootScope', '$parse', '$injector',
|
||||
function($rootScope, $parse_, $injector) {
|
||||
this.$get = ['$rootScope', '$parse', '$controller',
|
||||
function($rootScope, $parse_, $controller) {
|
||||
$parse = $parse_;
|
||||
/**
|
||||
* @ngdoc proprety
|
||||
|
|
@ -136,7 +136,7 @@ function $FormFactoryProvider() {
|
|||
|
||||
function formFactory(parent) {
|
||||
var scope = (parent || formFactory.rootForm).$new();
|
||||
$injector.instantiate(FormController, {$scope: scope});
|
||||
$controller(FormController, scope);
|
||||
return scope;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -63,8 +63,8 @@
|
|||
</doc:example>
|
||||
*/
|
||||
function $RouteProvider(){
|
||||
this.$get = ['$rootScope', '$location', '$routeParams', '$injector',
|
||||
function( $rootScope, $location, $routeParams, $injector) {
|
||||
this.$get = ['$rootScope', '$location', '$routeParams', '$controller',
|
||||
function( $rootScope, $location, $routeParams, $controller) {
|
||||
/**
|
||||
* @ngdoc event
|
||||
* @name angular.module.ng.$route#$beforeRouteChange
|
||||
|
|
@ -280,7 +280,7 @@ function $RouteProvider(){
|
|||
copy(next.params, $routeParams);
|
||||
next.scope = parentScope.$new();
|
||||
if (next.controller) {
|
||||
$injector.instantiate(next.controller, {$scope: next.scope});
|
||||
$controller(next.controller, next.scope);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
38
test/service/controllerSpec.js
Normal file
38
test/service/controllerSpec.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
'use strict';
|
||||
|
||||
describe('$controller', function() {
|
||||
var $controller;
|
||||
|
||||
beforeEach(inject(function($injector) {
|
||||
$controller = $injector.get('$controller');
|
||||
}));
|
||||
|
||||
it('should return instance of given controller class', function() {
|
||||
var MyClass = function() {},
|
||||
ctrl = $controller(MyClass);
|
||||
|
||||
expect(ctrl).toBeDefined();
|
||||
expect(ctrl instanceof MyClass).toBe(true);
|
||||
});
|
||||
|
||||
it('should inject arguments', inject(function($http) {
|
||||
var MyClass = function($http) {
|
||||
this.$http = $http;
|
||||
};
|
||||
|
||||
var ctrl = $controller(MyClass);
|
||||
expect(ctrl.$http).toBe($http);
|
||||
}));
|
||||
|
||||
|
||||
it('should inject given scope', function() {
|
||||
var MyClass = function($scope) {
|
||||
this.$scope = $scope;
|
||||
};
|
||||
|
||||
var scope = {},
|
||||
ctrl = $controller(MyClass, scope);
|
||||
|
||||
expect(ctrl.$scope).toBe(scope);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue