Skip to content

Commit a3136ae

Browse files
ciddanchristopherthielen
authored andcommitted
fix(Ng1ViewDeclaration): Make controllerProvider IInjectable (#3056)
Closes #3044. Changes the type of controllerProvider in Ng1ViewDeclaration to IInjectable. Adds 1 test for controller provider in ng1/stateSpec. Updates the controller handling test in ng1/viewSpec to verify that controllerProvider is IInjectable .
1 parent 2a45243 commit a3136ae

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/ng1/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export interface Ng1ViewDeclaration extends _ViewDeclaration {
360360
* }
361361
* ```
362362
*/
363-
controllerProvider?: Function;
363+
controllerProvider?: IInjectable;
364364

365365
/**
366366
* The scope variable name to use for resolve data.

test/ng1/stateSpec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ describe('state', function () {
9898
foo: function() { return 'Foo'; }
9999
}
100100
})
101+
.state('dynamicController', {
102+
url: "/dynamicController/:type",
103+
template: "a",
104+
controllerProvider: ["$stateParams", function($stateParams) {
105+
ctrlName = $stateParams.type + "Controller";
106+
return ctrlName;
107+
}]
108+
})
101109
.state('home.redirect', {
102110
url: "redir",
103111
onEnter: function($state) {
@@ -647,6 +655,12 @@ describe('state', function () {
647655
expect(template).toEqual("AcmeFooTemplate");
648656
}));
649657

658+
it('uses the controllerProvider to get controller dynamically', inject(function ($state, $q) {
659+
$state.transitionTo('dynamicController', { type: "Acme" });
660+
$q.flush();
661+
expect(ctrlName).toEqual("AcmeController");
662+
}));
663+
650664
it('updates the location #fragment, if specified', inject(function ($state, $q, $location) {
651665
// html5mode disabled
652666
locationProvider.html5Mode(false);
@@ -1076,7 +1090,7 @@ describe('state', function () {
10761090
var list = $state.get().sort(function(a, b) { return (a.name > b.name) - (b.name > a.name); });
10771091
var names = ['', 'A', 'B', 'C', 'D', 'DD', 'DDDD', 'E', 'F', 'H', 'HH', 'HHH', 'ISS2101', 'OPT', 'OPT.OPT2', 'RS', 'URLLESS',
10781092
'about', 'about.person', 'about.person.item', 'about.sidebar', 'about.sidebar.item',
1079-
'badParam', 'badParam2', 'dynamicTemplate', 'first', 'home', 'home.item', 'home.redirect',
1093+
'badParam', 'badParam2', 'dynamicController', 'dynamicTemplate', 'first', 'home', 'home.item', 'home.redirect',
10801094
'json', 'logA', 'logA.logB', 'logA.logB.logC', 'resolveFail', 'resolveTimeout',
10811095
'root', 'root.sub1', 'root.sub2', 'second'];
10821096

test/ng1/viewSpec.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {PathFactory} from "../../src/path/module";
1212
import {ng1ViewsBuilder, ng1ViewConfigFactory} from "../../src/ng1/statebuilders/views";
1313
import {ViewService} from "../../src/view/view";
1414
import {StateMatcher, StateBuilder} from "../../src/state/module";
15-
1615
import {State} from "../../src/state/module";
16+
import {Ng1StateDeclaration} from "../../src/ng1/interface";
1717

1818
describe('view', function() {
1919
var scope, $compile, $injector, elem, $controllerProvider, $urlMatcherFactoryProvider;
@@ -55,14 +55,16 @@ describe('view', function() {
5555
let state, path: PathNode[], ctrlExpression;
5656
beforeEach(() => {
5757
ctrlExpression = null;
58-
state = register({
58+
const stateDeclaration: Ng1StateDeclaration = {
5959
name: "foo",
6060
template: "test",
61-
controllerProvider: function (/* $stateParams, */ foo) { // todo: reimplement localized $stateParams
61+
controllerProvider: ["foo", function (/* $stateParams, */ foo) { // todo: reimplement localized $stateParams
6262
ctrlExpression = /* $stateParams.type + */ foo + "Controller as foo";
6363
return ctrlExpression;
64-
}
65-
});
64+
}]
65+
};
66+
67+
state = register(stateDeclaration);
6668
let $view = new ViewService();
6769
$view.viewConfigFactory("ng1", ng1ViewConfigFactory);
6870

0 commit comments

Comments
 (0)