无法使用角度承诺访问javascript本地函数


(function () {
'use strict';
angular
    .module('residentialApp')
    .factory('LockerBank', ['$q', 'lockerbankService', 'notificationService', 'rfc4122', 'LanguageResourceService', 'StringFormat'
        , function ($q, lockerbankService, notificationService, rfc4122, LanguageResourceService, StringFormat) {
            var lockerBank = function (lockerBankId, lockerName) {
                if (lockerName === undefined) {
                    lockerName = "";
                }
                self=this;
                if (lockerBankId === undefined) {
                    lockerBankId = "";
                }
                this.name = lockerName;
                this.id = lockerBankId;
                this.description = "";
                this.isActive = "";
                this.relayEndPointName = "";
                this.lockerBankCode = "";
                this.createdUtcDate = "";
                this.lastHeartBeatUtcDate = "";
                this.propertyId = "";
                this.mailRoomId = "";
                this.address = "";
                this.apiToken = "";
                this.status = "";
                this.lastHeatBeatDateDisplay = "";
                this.lastUpdateInSecond = "";
                this.createdOnDateDisplay = "";
                this.organisationId = "";
                return this;
            };
            lockerBank.prototype = {
                initialize: function () {
                },
                loadFromServer: function (propertyId) {
                    var defer = $q.defer();
                    if (this.id === undefined || this.id == null) {
                        defer.resolve(null);
                    }
                    else {
                        lockerbankService
                            .loadLockerBank(this.id)
                            .then(function success(data) {
                                // The problem is here
                                self.populateData(data);
                                var newObject = angular.copy(this);
                                defer.resolve(newObject);
                            }, function failure(response) {
                                defer.reject(response);
                            });
                    }
                    return defer.promise;
                },

                populateData: function (apiData) {
                    if (apiData === undefined || apiData == null) {
                        return;
                    }
                    if (this.id === undefined || this.id == null) {
                        this.id = apiData.Id;
                    }
                }
            };
            return lockerBank;
        }]);
})();

在上面的代码中,我无法从 then 函数访问 populateData 方法。

在函数的范围内,我得到 self=this 的对象是我已经在应用程序中创建的其他对象。

我认为范围存在问题。如果这不是角度承诺,那么我将按如下方式使用代码

lockerbankService.loadLockerBank(this.id)
.then(function success(data) {
   self.populateData(data);
   var newObject = angular.copy(this);
   defer.resolve(newObject);
}.bind(this))

任何人都可以帮助我解决这个范围问题吗?

我不能在函数中声明self loadFromServer。试试这个:

loadFromServer: function (propertyId) {
    var self = this;
    var defer = $q.defer();
    ....

最新更新