当AngularJS文件是JS和TS时,我正在尝试构建混合应用程序.
我似乎无法添加到JS控制器的路由.
我似乎无法添加到JS控制器的路由.
我依赖于以下example并执行以下操作:
const statesConfigBlock = ['$stateProvider',$stateProvider => { $stateProvider.state('main',{ url: '/main',templateUrl: 'app/components/layout/mainView.html',controller: 'mainController as main' }) }]; appModuleAngularJS.config(statesConfigBlock);
var app = angular.module('myApp',[]); (function(app) { 'use strict'; app.controller('mainController',[ function () { console.log("blah"); }]); })(app);
当我运行应用程序时,我得到:
The controller with the name ‘mainController’ is not registered
但是当我在控制台中运行时,我确实看到了它:
angular.module('myApp')._invokeQueue.filter(function(el){ return el[0] === "$controllerProvider"; }).map(function(el){ return el[2]["0"]; });
解决方法
好吧,我想我成功了.它可能不是最好的解决方案但是这里.
appDependencies = []; app = angular.module('myApp',appDependencies);
所有Angular 1.x控制器和服务都使用全局变量app,如下所示:
(function(app) { 'use strict'; app.controller('mainController',[ function () { console.log("blah"); }]); })(app);
最后,Angular 1.x模块ts文件使用全局应用程序并向其添加依赖项:
... declare const app: any; declare var appDependencies: any; export const appModuleAngularJS = app; angular.forEach([ uiRouter,upgradeModule.name ],module => { appDependencies.push(module); }); ... const statesConfigBlock = ['$stateProvider',controller: 'mainController as main' }) }]; appModuleAngularJS.config(statesConfigBlock); ...
现在,index.html文件中js导入的顺序非常重要.应该首先应用减速文件,然后是所有Angular 1.x控制器和服务,然后是转换为js文件的* .ts文件.
干杯!