【问题标题】:The sequence issue of AngularJS TypeScript ts files in VS2015 appBundle.jsVS2015 appBundle.js中AngularJS TypeScript ts文件的序列问题
【发布时间】:2015-09-04 21:15:08
【问题描述】:

我正在使用 VS2015 RC Cordova TypeScript 项目学习 AngularJS 和 TypeScript。

我在“index.ts”的同一个文件夹中添加了一个“MyController.ts”文件,下面的代码运行良好。

module MyTestApp{
   export class MyController
   {
          constructor( $scope )
          {
          $scope.message = { title: "Hello World!!" };
          }
   }
}
var myApp = angular.module( 'myTestApp', [] );         
myApp.controller( 'myController', MyTestApp.MyController );

但是,当我在“MyController.ts”的同一文件夹中添加另一个文件“YourController.ts”时

module MyTestApp{
   export class YourController
   {
          constructor( $scope )
          {
          $scope.message = { title: "Hello World!!" };
          }
   }
}

然后将此代码添加到 MyController.ts 的最后一行,因为控制器需要添加到 Angular 模块中。

myApp.controller( 'myController', MyTestApp.YourController);

我编译的项目没问题,但运行时出现错误消息。

原来这些ts文件都会按照ts文件的字母顺序依次编译成“appBundle.js”。 所以原因是“YourController.ts”在“MyController.ts”和这一行

 myApp.controller( 'myController', MyTestApp.YourController);

在“appBundle.js”文件中找不到上述代码下的“YourController”。

我知道这是因为 javascript 是一种顺序运行的脚本语言,我可以将以下代码剪切并粘贴到“YourController.ts”中。

 var myApp = angular.module( 'myTestApp', [] );
 myApp.controller( 'myController', MyTestApp.MyController );
 myApp.controller( 'myController', MyTestApp.YourController);

但是如果下次我添加另一个控制器,比如“ZooController.ts”,我应该将以上所有代码移到“ZooController”的最后一行.ts”?

谁能建议我放置上述代码的合适位置在哪里?

非常感谢。

【问题讨论】:

    标签: angularjs cordova typescript visual-studio-2015


    【解决方案1】:

    不推荐使用自动捆绑,因为out 有一些不良后果记录在这里:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md

    也就是说,您可以使用 grunt-ts 之类的东西来减轻痛苦:https://github.com/TypeStrong/grunt-ts#javascript-generation

    【讨论】:

    • 感谢您的建议。但不知道我是否可以直接从 VS2015 进行任何设置。
    • 哦。经过测试,我发现ts文件中的“引用路径”可以解决“appBundle.js”文件中的序列问题。 ///
    猜你喜欢
    • 2015-10-22
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多