From a3136aedb85c0ec1117710b02aa7e6f4aeb8c11a Mon Sep 17 00:00:00 2001 From: Mikael Selander Date: Sat, 1 Oct 2016 18:05:41 +0200 Subject: [PATCH] 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 . --- src/ng1/interface.ts | 2 +- test/ng1/stateSpec.js | 16 +++++++++++++++- test/ng1/viewSpec.ts | 12 +++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/ng1/interface.ts b/src/ng1/interface.ts index 05546d49a..d7639cafc 100644 --- a/src/ng1/interface.ts +++ b/src/ng1/interface.ts @@ -360,7 +360,7 @@ export interface Ng1ViewDeclaration extends _ViewDeclaration { * } * ``` */ - controllerProvider?: Function; + controllerProvider?: IInjectable; /** * The scope variable name to use for resolve data. diff --git a/test/ng1/stateSpec.js b/test/ng1/stateSpec.js index 3059a2008..85ae83835 100644 --- a/test/ng1/stateSpec.js +++ b/test/ng1/stateSpec.js @@ -98,6 +98,14 @@ describe('state', function () { foo: function() { return 'Foo'; } } }) + .state('dynamicController', { + url: "/dynamicController/:type", + template: "a", + controllerProvider: ["$stateParams", function($stateParams) { + ctrlName = $stateParams.type + "Controller"; + return ctrlName; + }] + }) .state('home.redirect', { url: "redir", onEnter: function($state) { @@ -647,6 +655,12 @@ describe('state', function () { expect(template).toEqual("AcmeFooTemplate"); })); + it('uses the controllerProvider to get controller dynamically', inject(function ($state, $q) { + $state.transitionTo('dynamicController', { type: "Acme" }); + $q.flush(); + expect(ctrlName).toEqual("AcmeController"); + })); + it('updates the location #fragment, if specified', inject(function ($state, $q, $location) { // html5mode disabled locationProvider.html5Mode(false); @@ -1076,7 +1090,7 @@ describe('state', function () { var list = $state.get().sort(function(a, b) { return (a.name > b.name) - (b.name > a.name); }); var names = ['', 'A', 'B', 'C', 'D', 'DD', 'DDDD', 'E', 'F', 'H', 'HH', 'HHH', 'ISS2101', 'OPT', 'OPT.OPT2', 'RS', 'URLLESS', 'about', 'about.person', 'about.person.item', 'about.sidebar', 'about.sidebar.item', - 'badParam', 'badParam2', 'dynamicTemplate', 'first', 'home', 'home.item', 'home.redirect', + 'badParam', 'badParam2', 'dynamicController', 'dynamicTemplate', 'first', 'home', 'home.item', 'home.redirect', 'json', 'logA', 'logA.logB', 'logA.logB.logC', 'resolveFail', 'resolveTimeout', 'root', 'root.sub1', 'root.sub2', 'second']; diff --git a/test/ng1/viewSpec.ts b/test/ng1/viewSpec.ts index 60a29411b..c9eeb9efd 100644 --- a/test/ng1/viewSpec.ts +++ b/test/ng1/viewSpec.ts @@ -12,8 +12,8 @@ import {PathFactory} from "../../src/path/module"; import {ng1ViewsBuilder, ng1ViewConfigFactory} from "../../src/ng1/statebuilders/views"; import {ViewService} from "../../src/view/view"; import {StateMatcher, StateBuilder} from "../../src/state/module"; - import {State} from "../../src/state/module"; +import {Ng1StateDeclaration} from "../../src/ng1/interface"; describe('view', function() { var scope, $compile, $injector, elem, $controllerProvider, $urlMatcherFactoryProvider; @@ -55,14 +55,16 @@ describe('view', function() { let state, path: PathNode[], ctrlExpression; beforeEach(() => { ctrlExpression = null; - state = register({ + const stateDeclaration: Ng1StateDeclaration = { name: "foo", template: "test", - controllerProvider: function (/* $stateParams, */ foo) { // todo: reimplement localized $stateParams + controllerProvider: ["foo", function (/* $stateParams, */ foo) { // todo: reimplement localized $stateParams ctrlExpression = /* $stateParams.type + */ foo + "Controller as foo"; return ctrlExpression; - } - }); + }] + }; + + state = register(stateDeclaration); let $view = new ViewService(); $view.viewConfigFactory("ng1", ng1ViewConfigFactory);