以下は、これまでに私のコードです:
マイモジュール
module App.SomeModule {
import ILabelSettingsViewModel = App.GeneralSettings.data.ILabelSettingsViewModel;
import IGeneralSettingsService = App.GeneralSettingsService.IGeneralSettingsService;
export enum LabelPageFormat {
A4,
Thermal
}
export interface IConsignmentDataService {
getAccountLabelFormat(): LabelPageFormat;
}
export class MyDataService implements IMyDataService {
accountLabelFormat: LabelPageFormat;
static $inject: string[] = ["generalSettingsService"];
constructor(private generalSettingsService: IGeneralSettingsService) {
this.determineAccountLabelFormat();
}
getAccountLabelFormat(): LabelPageFormat {
return this.accountLabelFormat;
}
private determineAccountLabelFormat() {
var that = this;
this.generalSettingsService.getLabelSettings().then((data: ILabelSettingsViewModel) => {
switch (data.name) {
case LabelPageFormat[LabelPageFormat.Thermal]:
that.accountLabelFormat = LabelPageFormat.Thermal;
break;
default:
that.accountLabelFormat = LabelPageFormat.A4;
break;
}
},() => {
that.accountLabelFormat = LabelPageFormat.A4;
});
}
}
angular.module("app.common").service("myDataService", MyDataService);
}
と私のコントローラ
module App.Consignment.List {
"use strict";
import IConsignmentDataService = Consignment.IConsignmentDataService;
import ConsignmentListGridScope = Consignment.IConsignmentListGridScope;
class ConsignmentListController implements IConsignmentBulkActionProvider {
accountLabelFormat: LabelPageFormat;
static $inject = ["$scope", "myDataService"];
constructor(private $scope: ConsignmentListGridScope, private myDataService: IMyDataService) {
this.accountLabelFormat = this.consignmentDataService.getAccountLabelFormat();
}
}
angular.module("app.consignment").controller("consignmentListController", ConsignmentListController);
}
私は何をしようとしていますで、私のデータサービスからaccountLabelFormatを取得し、他の場所にそれを使用してください。データサービスでは、メソッドを使用してデータベースからフォーマットを取得します。これは約束として返され、成功した場合はコントローラからgetAccountLabelFormat()
メソッドを呼び出すと返される変数を設定しています。私の問題は、サービスメソッドが非同期であるため、getAccountLabelFormat()
メソッドを呼び出すと、accountLabelFormat
サービスの変数がまだ設定されていないため、コントローラで未定義の値が得られるたびに問題になります。どのように私はこれを解決することができますに関する任意のアイデア?前もって感謝します。
をチェーン。 '$ q.when() 'を使う代わりに、私は自分のサービスから' promise'を返し、最終的に価値を得るために私のコントローラの中で 'promise'を処理しました。とにかく助けてくれてありがとう:) –