我目前正在开发一个Angular JS项目,我在其中调用webservice,然后在成功回调中更新基于结果的文本框值。
现在,我的web服务被调用有点迟,成功回调需要一些时间。在成功回调中,我更新了一个文本框值,并隐藏了一个加载进度对话框。
但是,如果我不使用$scope.apply()
,则不会隐藏进度对话框,也不会更新文本框。
如果使用相同,则应用它。 $scope.apply()
的目的是什么。
使用它的最佳做法是什么?它可以用在像我这样的情况下。我也尝试过使用$timeout
。即便如此,但我认为这不是一种首选方法。
这是我的代码
//服务实现
app.service('registerService', function ($http, APP_CONFIG, $q, spinnerService) {
this.callService = function (request) {
spinnerService.show('mySpinner')
var deferred = $q.defer();
$http({
method: 'POST',
url: APP_CONFIG.baseUrl + '/register',
data: request
}).success(function (data) {
deferred.resolve(data);
}).error(function () {
deferred.reject('There was an error while making request');
});
return deferred.promise;
};
});
//从控制器内部调用服务
registerService.callService(JSON.stringify(requestData)).then(function (data) {
$scope.$apply(function () {
spinnerService.hide('mySpinner');//hide loading..this works!
});
spinnerService.hide('mySpinner');//hide loading ..this does not if I remove $scope.apply()
}, function () {
//unable to fetch NLS resource
spinnerService.hide('mySpinner');//hide loading
});
}
// spinner service implemtation
app.factory('spinnerService', function () {
var cache = {};
return {
// A private function intended for spinner directives to register themselves with the service.
_register: function (spinnerScope) {
// If no id is passed in, throw an exception.
if (!spinnerScope.id) {
throw new Error("A spinner must have an ID to register with the spinner service.");
}
// Add our spinner directive's scope to the cache.
cache[spinnerScope.id] = spinnerScope;
},
// A private function exposed just in case the user really needs to manually unregister a spinner.
_unregister: function (spinnerId) {
delete cache[spinnerId];
},
// A private function that will remove an entire spinner group if needed.
_unregisterGroup: function (group) {
for (var spinnerId in cache) {
if (cache.hasOwnProperty(spinnerId)) {
if (cache[spinnerId].group === group) {
delete cache[spinnerId];
}
}
}
},
// A private function that will clear out all spinners from the cache.
_unregisterAll: function () {
for (var spinnerId in cache) {
if (cache.hasOwnProperty(pinnerId)) {
delete cache[spinnerId];
}
}
},
// Show the specified spinner.
// If loadingText is specified, replace the loadingText specified on the directive as we show the spinner.
show: function (spinnerId, loadingText) {
$("body").find("#loading").addClass("mydiv");
if (cache.hasOwnProperty(spinnerId)) {
var spinnerScope = cache[spinnerId];
spinnerScope.showSpinner = true;
if (loadingText !== undefined) {
spinnerScope.loadingText = loadingText;
}
}
},
// Hide the specified spinner.
// If doneText is specified, replace the doneText specified on the directive as we hide the spinner.
hide: function (spinnerId, doneText) {
if (cache.hasOwnProperty(spinnerId)) {
var spinnerScope = cache[spinnerId];
$("body").find("#loading").removeClass("mydiv");
spinnerScope.showSpinner = false;
if (doneText !== undefined) {
spinnerScope.doneText = doneText;
}
}
}
};
});