有人知道如何在視圖更改時使角度激發成爲事件嗎?或者當請求並下載視圖時正確?我試圖爲頁面更改時添加加載動畫。查看視圖在angularjs中更改時的視圖
回答
看看this thread它看起來像$httpProvider.responseInterceptors
是一個很好的地方添加這種類型的東西。
This fiddle顯示了一個很好的示例,說明在哪裏添加代碼以啓動/停止ajax請求的微調器。 This fiddle是類似的,但實際上顯示並隱藏了「加載...」div。
如果你只是想顯示微調時的看法改變,你可以限制你的開始/停止代碼時content-type
等於text/html
類似於顯示了application/json
this post來。
注意:在我的測試中,當檢索我的.html文件時,spinnerFunction
中的headersGetter()['Content-Type']
被忽略,而在進行服務調用時它被填充。
我認爲一個更簡單,適應性更強的方法是使用AngularJS $http.pendingRequests.length
調用。它返回掛起的ajax呼叫計數。所以當它達到0
時,你的頁面已經完成加載。
您可以創建一個指令,在任何元素上插入加載div(scrim),然後等待所有ajax調用來解析並刪除您的微調器。
下面的代碼的肉,讓您的AngularJS指令:
// Prepend the loading div to the dom element that this directive is applied.
var scrim = $('<div id="loading"></div>');
$(scrim).prependTo(domElement);
/**
* returns the number of active ajax requests (angular + jquery)
* $.active returns the count of jQuery specific unresolved ajax calls. It becomes 0 if
* there are no calls to be made or when calls become finished.
* @return number of active requests.
*/
function totalActiveAjaxRequests() {
return ($http.pendingRequests.length + $.active);
}
/**
* Check for completion of pending Ajax requests. When they're done,
* remove the loading screen and show the page data.
*/
scope.$watch(totalActiveAjaxRequests, function whenActiveAjaxRequestsChange() {
if(totalActiveAjaxRequests() === 0){
$log.log("done loading ajax requests");
$(scrim).remove();
$(domElement).css("position", "inherit");
}
});
請注意,$。主動是未公開。
謝謝你!〜 – matsko 2012-11-05 23:11:33
任何時候:)我只是在我的頭上打了幾個小時,然後想到我會分享。 – Stone 2012-11-05 23:47:24
順便說一句,你也可以使用'$ scope。$ watch'來達到同樣的效果,而不使用'setTimeout'。這將是一個更好的選擇,因爲這符合$範圍摘要。你也可以使用這個插件,但是當你完成的時候你必須手動調用'$ scope.onReady()'。 https://github.com/yearofmoo/AngularJS-Scope.onReady – matsko 2012-11-05 23:50:57
- 1. 在查看視圖時更新UITableView?
- 2. 視圖在列表視圖中查看
- 3. 在AngularJS中動態更改視圖
- 4. AngularJS更新視圖時$ rootScope改變
- 5. 在網格視圖中更改視圖
- 6. 更改圖像視圖的動態圖像查看
- 7. 在ng視圖中更改視圖時,自定義指令不起作用,angularjs
- 8. 在視圖中查看(Android)
- 9. iCarousel在視圖中查看?
- 10. 如何在Android中滾動視圖時查看視圖?
- 11. 在angularjs視圖外進行更改
- 12. 查看錶格視圖後的單元格圖像更改
- 13. 如何在iPhone中更改方向時更改視圖控制器的視圖?
- 14. 從子視圖中更改視圖
- 15. 如何在AngularJS中更改路線時防止視圖重繪
- 16. 查看當表或視圖最後更改時,sql server 2000
- 17. 在基於視圖的表視圖中查看基於表的視圖
- 18. 在NSSplitViewController中更改視圖
- 19. 在android中更改視圖
- 20. 在android中更改視圖
- 21. 在UIViewController中更改視圖
- 22. 更改視圖
- 23. 更改視圖
- 24. 在視圖內查看
- 25. 打開Modal並更改URL,而不更改AngularJS中的視圖
- 26. 更改視圖和UI與AngularJS
- 27. AngularJS更改URL但不是視圖
- 28. 更改視圖啓動時
- 29. 查看:包含在一個視圖中的多個視圖
- 30. 在視圖之間導航時更改表視圖的位置
完美。正是我需要的。謝謝:) – matsko 2012-08-03 16:25:20
這隻適用於你有一個單一的Ajax請求。 – Meeker 2013-10-22 04:07:41
目前是1.2嗎? – ericpeters0n 2014-03-09 04:52:28