编译后,模块内部声明的变量未定义

时间:2015-11-04 11:34:52

标签: angularjs typescript typescript1.6

我已声明以下文件

app.types.ts

/// <reference path="../../typings/tsd.d.ts"/>
module App{
    export var Module =     "website";
    //---------------Controller Base Types---------------
    export interface IScope extends ng.IScope {
        events: Controller;
    }
    export class Controller{
        protected scope: IScope;
        static id       : string;
        constructor($scope: IScope,controllerId:  string){
            this.scope        =     $scope;
            this.scope.events =     this;
            Controller.id                =      controllerId;
        }
    }
}

app.ts

    /// /// <reference path="app.types.ts"/>
    module App {
    "use strict";
    var app = angular.module(Module, [
        // Angular modules
        'ngMessages','ngAnimate','ngTouch','ngAria','ngSanitize',
        // Custom modules
        'ui.router','ui.bootstrap','ui.bootstrap.tpls'
        // 3rd Party Modules
    ]);
}

代码编译成Js而没有错误,但变量Module在文件中未定义。

我也尝试过使用App.Module,但变量仍未定义

更新: 我检查app.types.js是否插入到app.js之前的html中。生成的Js文件似乎不正确。

//App.js
(function(){/// <reference path="app.types.ts"/>
var App;
(function (App) {
    "use strict";
    var app = angular.module(App.Module, [
        // Angular modules
        'ngMessages', 'ngAnimate', 'ngTouch', 'ngAria', 'ngSanitize',
        // Custom modules
        'ui.router', 'ui.bootstrap', 'ui.bootstrap.tpls'
    ]);
})(App || (App = {}));
}());

//App.types.js
(function(){/// <reference path="../../typings/tsd.d.ts"/>
var App;
(function (App) {
    App.Module = "website";
    var Controller = (function () {
        function Controller($scope, controllerId) {
            this.scope = $scope;
            this.scope.events = this;
            Controller.id = controllerId;
        }
        return Controller;
    })();
    App.Controller = Controller;
})(App || (App = {}));
}());

1 个答案:

答案 0 :(得分:0)

你应该添加:

/// <reference path="relative/path/to/app.types.ts"/>

到您的app.ts文件。如果我没有弄错,您应该会看到脚本的顺序不正确 - 即在app.types.js之后加载app.js