我正在使用带有显示模块模式的 Angular 服务。该服务在内部从 Web 服务中提取字符串资源,并通过“字符串”公共变量使它们可用。我必须初始化私有“字符串”变量,因为它在进行服务调用之前被引用。
我从服务中获取正确的字符串数据并将其复制到私有“字符串”变量。但是,当客户端引用公共“字符串”时,它仍然保留其原始值。
知道我做错了什么,或者如何让公共“字符串”更新吗?
'use strict';
io1App.factory('Resources', ['$rootScope', 'DataService', '$q',
function ($rootScope, DataService, $q) {
var urlBase = '/api/sfc/resource';
// Need to pre-define 'ERROR_HEADER', since it is referenced in Index.html...before we have a chance to download it from the server.
var strings = {
'ERROR_HEADER': 'Error!'
};
var getStringResources = function (locale) {
var url = urlBase + '/' + locale;
var deferred = $q.defer();
var promise = DataService.GetMethod(url);
// Note that DataService.GetMethod(...) is returning a $q promise
promise.then(function (data) {
strings = data;
deferred.resolve();
},
function (err) {
deferred.reject(err);
});
return deferred.promise;
};
return {
Strings: strings,
GetStringResources: getStringResources
}
}]);
服务调用将返回的数据设置为私有“字符串”变量
promise.then(function (data) {
strings = data;
deferred.resolve();
},
私有“字符串”现在显示以下内容(通过 Chrome 开发人员工具):
strings = {
'ERROR_HEADER': 'Error!'
'INVALID_PROCESS_ORDER': 'Process Order [%d] could not be entered because it does not belong to Manfacturing Order [%d]',
'DUPLICATE_PROCESS_ORDER': 'Process Order [%d] already entered.',
'USER_NOT_ITAR': 'Manufacturing order [%d] is ITAR, and you are not ITAR approved. You cannot proceed with this order. Please contact your supervisor.'
};
然而,当在 Angular 控制器中引用公共“字符串”时,“字符串”仍在引用私有“字符串”的原始值。
资源.字符串:
{
'ERROR_HEADER': 'Error!'
};
有什么建议么?