如何等到一个变量被定义时才使用$scope $watch ?

时间:2022-06-15 12:02:21

Below is my code so far:
My Module

下面是我到目前为止的代码:我的模块

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);
}     

and my controller

我的控制器

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);
}

what I am trying to do is, get the accountLabelFormat from my data service and then use it to somewhere else. In data service, a method is used to get the format from database which is returned as a promise and then if success, I am setting the variable that will be returned when I call the getAccountLabelFormat() method from my controller. Now my problem is, as the service method is async, by the time I call the getAccountLabelFormat() method, the variable in accountLabelFormat service was not yet set, so that every time I got an undefined value in my controller. Any ideas about how can I solve this? Thanks in advance.

我要做的是,从我的数据服务中获取accountLabelFormat,然后将其用于其他地方。在data service中,一个方法用于从数据库获取格式,该格式作为一个承诺返回,如果成功,我将设置当我从控制器调用getAccountLabelFormat()方法时返回的变量。现在我的问题是,由于服务方法是异步的,当我调用getAccountLabelFormat()方法时,accountLabelFormat服务中的变量还没有设置,因此每次我在控制器中获得一个未定义的值时。有什么办法解决这个问题吗?提前谢谢。

1 个解决方案

#1


2  

use $q.when. check out https://docs.angularjs.org/api/ng/service/$q

使用$下载图片。看看https://docs.angularjs.org/api/ng/service/问美元

For example:

例如:

$q.when(this.accountLabelFormat)

so when you ask for that value it will return a promise then just chain it a then statement

所以当你要求这个值时,它会返回一个承诺,然后把它链接起来。

#1


2  

use $q.when. check out https://docs.angularjs.org/api/ng/service/$q

使用$下载图片。看看https://docs.angularjs.org/api/ng/service/问美元

For example:

例如:

$q.when(this.accountLabelFormat)

so when you ask for that value it will return a promise then just chain it a then statement

所以当你要求这个值时,它会返回一个承诺,然后把它链接起来。