简体   繁体   English

混合Angular 1.x + Angular 6应用程序,包含Angular 1.x中的vanilla JS和TS文件

[英]Hybrid Angular 1.x + Angular 6 App with both vanilla JS and TS files in Angular 1.x

I'm trying to build a hybrid app when the AngularJS files are both JS and TS. 当AngularJS文件是JS和TS时,我正在尝试构建混合应用程序。 I can't seem to add a route to a JS controller. 我似乎无法添加到JS控制器的路由。

I'm relying on the following example and doing the following: 我依赖以下示例并执行以下操作:

const statesConfigBlock = ['$stateProvider', $stateProvider => {
  $stateProvider.state('main', {
    url: '/main',
    templateUrl: 'app/components/layout/mainView.html',
    controller: 'mainController as main'
  })
}];
appModuleAngularJS.config(statesConfigBlock);

while I have a mainCtrl.js file that's defined as: 虽然我有一个mainCtrl.js文件定义为:

var app = angular.module('myApp', []);

(function(app) {
  'use strict';

  app.controller('mainController', [
      function () {
        console.log("blah");

      }]);
})(app);

when I run the app I get: 当我运行应用程序时,我得到:

The controller with the name 'mainController' is not registered 名称为“mainController”的控制器未注册

but I do see it when I run in console: 但是当我在控制台中运行时,我确实看到了它:

angular.module('myApp')._invokeQueue.filter(function(el){
  return el[0] === "$controllerProvider";
}).map(function(el){
  return el[2]["0"];
});

Okay, I think I managed to make it work. 好吧,我想我成功了。 It might not be the best solution but here goes. 它可能不是最好的解决方案但是这里。

First, I created a js file that holds the declaration of the module: 首先,我创建了一个包含模块声明的js文件:

appDependencies = [];
app = angular.module('myApp', appDependencies);

All the Angular 1.x controllers and services use the global variable app like so: 所有Angular 1.x控制器和服务都使用全局变量app如下所示:

(function(app) {
  'use strict';

  app.controller('mainController', [
      function () {
        console.log("blah");

      }]);
})(app);

Finally, the Angular 1.x module ts file uses the global app and adds dependencies to it: 最后,Angular 1.x模块ts文件使用全局app并向其添加依赖项:

...

declare const app: any;
declare var appDependencies: any;

export const appModuleAngularJS = app;

angular.forEach([
  uiRouter, upgradeModule.name
], module => {
  appDependencies.push(module);
});

...

const statesConfigBlock = ['$stateProvider', $stateProvider => {
  $stateProvider.state('main', {
    url: '/main',
    templateUrl: 'app/components/layout/mainView.html',
    controller: 'mainController as main'
  })
}];
appModuleAngularJS.config(statesConfigBlock);

...

Now, the order of the js imports in the index.html file is really important. 现在, index.html文件中js导入的顺序非常重要。 The app deceleration file should come first, then all the Angular 1.x controllers and services and then the *.ts files that were transpiled to a js file. 应该首先app减速文件,然后是所有Angular 1.x控制器和服务,然后是转换为js文件的* .ts文件。

This solution worked for me, but I'm more than happy to read a better one. 这个解决方案对我有用,但我非常乐意阅读更好的解决方案。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM