我在從SQL Server中獲取並顯示數據庫列表的控制器中有類似的問題。我希望顯示等待光標,直到數據返回,但UI被凍結。我通過使用$rootScope
方法(導致等待遊標顯示在視圖中)切換isWaiting
標誌,然後在異步過程結束時清除回調中的該標誌來解決此問題。
這不完全是你想要做的,但它可能足夠接近以適應。您可能可以使用$scope
而不是$rootScope
用於您的目的的方法,並且具有控制是否顯示微調器圖像的div級別(而不是html級別)。
聲明:這是用於使用node-webkit的桌面應用程序,而不是普通的瀏覽器。但我認爲這種基本方法也可以在普通瀏覽器中運行。
CSS:
html.wait, html.wait * {
cursor: wait !important;
}
MainController:
// sets up a method that can be used from any controller
$rootScope.toggleWaiting = function (isWaiting) {
$rootScope.isWaiting = isWaiting;
$('html')[isWaiting ? 'addClass' : 'removeClass']('wait');
};
DatabasesController:
.controller('databasesController', ['$rootScope', '$scope', function($rootScope, $scope) {
$rootScope.toggleWaiting(true);
var SqlServerHelper = require("SqlServerHelper");
var sqlServerHelper = new SqlServerHelper(/* sensitive config info removed */);
sqlServerHelper.getDatabases(function(err, results) {
/* standard error handling removed */
$scope.$apply(function() {
$rootScope.toggleWaiting(false);
$scope.databaseList = results;
});
});
}])
附:最後一個想法是:如果gif仍然出現凍結,那麼可以使用控制器中的$timeout
來啓動長時間運行的進程,因此它會發生在異步後臺線程上。 (雖然我會認爲這是幕後的工作方式,取決於它究竟做了些什麼,需要這麼長時間......大概是以異步方式調用外部資源開始?)
不,不會調用外部資源,因爲正如您所說,它是異步的。問題在於角度編譯DOM _synchroniously_並且在那段時間內不會將控制權返回給瀏覽器。您的解決方案似乎是針對異步情況,而不是我需要的。 – 2014-11-01 21:40:17