Javascript 何时为webservice回调调用$scope.apply()
我目前正在开发一个Angular JS项目,在该项目中,我调用一个webservice,然后在成功回调中根据结果更新一个文本框值 现在,我的Web服务调用有点晚,成功回调需要一些时间。在成功回调中,我更新了一个文本框值,并隐藏了一个加载进度对话框 但是,如果我不使用Javascript 何时为webservice回调调用$scope.apply(),javascript,jquery,angularjs,web-services,Javascript,Jquery,Angularjs,Web Services,我目前正在开发一个Angular JS项目,在该项目中,我调用一个webservice,然后在成功回调中根据结果更新一个文本框值 现在,我的Web服务调用有点晚,成功回调需要一些时间。在成功回调中,我更新了一个文本框值,并隐藏了一个加载进度对话框 但是,如果我不使用$scope.apply(),则进度对话框永远不会隐藏,文本框也不会更新 如果使用相同的,则应用它。$scope.apply()的用途是什么 使用它的最佳实践是什么。它能用在像我这样的场合吗。我也尝试过使用$timeout。即使这样也
$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
});
}
//微调器服务实现
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;
}
}
}
};
})) 不必对从内部触发的回调使用
$apply()
。。。为什么要在承诺中包装$http
?不管怎样,它都会给你一个承诺也许阅读并清理代码以遵循最佳实践,您的问题可能会消失。如果我没有明确返回承诺,那么控件将如何移回我的控制器,从那里我可以更新我的$scope或任何UI内容?只需让您的callService返回$http(…)
控制器会很好地处理它。代码中有一个基本问题,即您试图缓存范围对象,然后尝试在工厂内更新范围,这被认为不是一个好的做法,您应该尝试这样做:ok@NarainMittal。我明白你的意思。那么您能告诉我$scope.apply()是在什么条件下使用的吗?为什么?