Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until a variable is defined without using $scope $watch in angular?

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.

like image 541
vpv Avatar asked Sep 08 '16 01:09

vpv


People also ask

Why is $Watch used in angular?

The angular JS $watch function is used to watch the scope object. The $watch keep an eye on the variable and as the value of the variable changes the angular JS $what runs a function.

Why $scope is used in AngularJS?

Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the "AngularJS realm" (controllers, services, AngularJS event handlers). Scopes can be nested to limit access to the properties of application components while providing access to shared model properties.

What is the difference between $scope and this in AngularJS?

"How does this and $scope work in AngularJS controllers?" When the controller constructor function is called, this is the controller. When a function defined on a $scope object is called, this is the "scope in effect when the function was called". This may (or may not!) be the $scope that the function is defined on.

What is the use of $Watch in AngularJS?

$watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in Angularjs to handle variable changes in application.


1 Answers

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

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

like image 177
atsituab Avatar answered Nov 01 '22 15:11

atsituab