0

我需要创建一个对象。类及其属性和对象创建发生在不同的文件上。

这是包含类的文件

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 显示为未定义的对象。它没有初始化。如果我为这件事使用一个文件(没有两个文件),它的效果很好。有谁知道该怎么做以及这样做的原因?

4

1 回答 1

1

当您希望将两个打字稿文件合并到一个 javascript 文件中时,您需要指定 --out 编译器标志。

否则,每个 typescript 文件都会被编译成一个单独的 javascript 文件,您负责以正确的顺序加载它们。

于 2013-08-19T12:48:54.773 回答