type在typeScript上是未定义的

时间:2013-08-19 12:10:56

标签: class angularjs undefined typescript asp.net-web-api

我需要创建一个对象。班级&它们的属性和对象创建发生在不同的文件上。

这是包含类

的文件
module Model {

export class DonationServiceResponse {
    ErrorMessage: string;
    ErrorCode: number;
    Comapny: string;
    StreateName: string;
    IsAnonymous: boolean;
    IsOneOffDonation: boolean;
    DonationIntentType: number;
    EmployeeId: number;
    Amount: number;
    LogoImage: string;
    Id: number;
}}

在下面的文件对象创建中发生了

  /// <reference path="DonationServiceResponse.ts" />
/// <reference path="../../typings/angularjs/angular.d.ts" />

angular.module('myApp', []);

interface IDonationServiceController {
    Tiles: Array;
    TileContainerEntities: Array;
    TotalAmount: number;
}


class TileContainerEntity  {

    DonationContainer: test.DonationServiceResponse;
    httpService: any;
    scope: any;
    res: boolean;
    AnonymousText: string;
    OneTimeText: string;

    constructor(donationAmount: number, charityLogo: string, anonymous: bool, oneTime: bool, id: number, employeeId: number, http: ng.IHttpService, scope: ng.IHttpService) {

        this.DonationContainer = new test.DonationServiceResponse();
        this.DonationContainer.Amount = donationAmount;
        this.DonationContainer.LogoImage = charityLogo;
        this.DonationContainer.Id = id;
        this.DonationContainer.EmployeeId = employeeId;
        this.httpService = http;
        this.scope = scope;
    }

}

class DonationServiceController implements IDonationServiceController {
    private httpService: any;
    TotalAmount: number = 0;
    Tiles: TileContainerEntity[];
    TileContainerEntities: TileContainerEntity[];

    constructor($scope, $http: ng.IHttpService) {
        this.httpService = $http;
        $scope.VM = this;
        this.getAllTiles($scope.VM, $http);
    }

    getAllTiles(scope, http): void {
        this.httpService.get("/API/PrivateAPI/test").success(function (data, status) {
            this.TileContainerEntities = [];

            var num = 0;

            for (var index = 0; index < data.length; index++) {
                var amount = data[index].Amount;
                var chairtyLogo = data[index].LogoImage;
                var isAnonymousOff = data[index].IsAnonymous;
                var isOneOff = data[index].IsOneOffDonation;
                var id = data[index].Id;
                var empId = data[index].EmployeeId;
                var tileEntity = new TileContainerEntity(amount, chairtyLogo, isAnonymousOff, isOneOff, id, empId, http, scope);
                this.TileContainerEntities.push(tileEntity);
                num = num + data[index].Amount;
            }
            scope.Tiles = this.TileContainerEntities;
            scope.TotalAmount = num;

        });
    }
}

我的问题在于构造函数DonationContainer显示为未定义的对象。它没有初始化。如果我使用一个文件来处理这个东西(没有两个文件),那么它的效果很好。有人知道怎么做并且有理由吗?

1 个答案:

答案 0 :(得分:1)

如果要将两个打字稿文件合并到一个javascript文件中,则需要指定--out编译器标志。

否则每个打字稿文件都会编译成一个单独的javascript文件,你负责按正确的顺序加载它们。