如何重置$timeout
,並禁用$watch()
?
2
A
回答
2
雙方的關鍵是分配的結果是:
var myTimer = $timeout(function() { /* ... */ }, 5000);
$timeout.cancel(yourTimer);
您可以通過註銷函數返回清除$watch()
該函數爲一個變量。
清理超時,只是「.cancel()」是:
var customTimeout = $timeout(function() {
// arbitrary code
}, 55);
$timeout.cancel(customTimeout);
這同樣適用於「$間隔()」。
要禁用手錶,只需調用它即可。
var deregisterWatchFn = $rootScope.$watch(‘someGloballyAvailableProperty’, function (newVal) {
if (newVal) {
// we invoke that deregistration function, to disable the watch
deregisterWatchFn();
...
}
});
0
您可以通過使用$timeout.cancel
清除$timeout
像這樣:
var dereg = $scope.$watch(function() { /* ... */ });
dereg(); // watcher is now gone
0
你已經得到了大部分答案,但另一點可能是有用的,你應該經常清理在相關範圍內被破壞的手錶,所以下面的模式是非常有用的:
$scope.$on('$destroy', $scope.$watch(function() { /* ... */ });
這將確保$watch
總是在銷燬範圍時自動取消。
當然,如果你想手動銷燬手錶,你也必須保存結果,以便你可以調用它,但是這種模式可以讓你在大多數情況下避免這種情況。
相關問題
- 1. AngularJS - $ watch和$ timeout
- 2. Scope。$ watch with $ timeout
- 3. AngularJS global $ http timeout
- 4. 如何使用AngularJS的$ timeout內的$ scope?
- 5. angularjs:$ watch not triggered
- 6. AngularJS:$ evalAsync vs $ timeout
- 7. AngularJS Clock Factory,$ timeout woes
- 8. 如何在angularjs組件中使用$ watch
- 9. 如何使用$ watch攻破Angularjs?
- 10. AngularJS watch FileReader()。readyState change
- 11. 如何使用AngularJS重置
- 12. 如何禁用AngularJS的閃爍 - 重複?
- 13. $ watch不叫,Angularjs
- 14. AngularJS $ scope。$ on()和$ timeout()
- 15. AngularJS - 觸發$ watch/$觀察者
- 16. AngularJS $ watch newValue未定義
- 17. 如何在AngularJS中停止$ timeout?
- 18. AngularJS:爲每個頁面切換重置$ timeout
- 19. AngularJS的$ watch函數如何工作?
- 20. angularjs 1 ngModel two $ watch
- 21. 如何禁用Android設置重置?
- 22. AngularJS中的$ timeout中的$ location
- 23. AngularJS:$ watch vs controller save()
- 24. 何時在AngularJS中使用$ watch
- 25. angularjs $ watch舊值和新值相同
- 26. AngularJS - $ timeout裏面的請求
- 27. 用top,ps,watch和timeout監視服務器
- 28. AngularJS $ rootScope.authenticated重置?
- 29. AngularJS:$ watch - 加快速度
- 30. 如何設置值,並使用angularjs
我明白了,謝謝。 – JackinC
答案的來源可能是 - https://www.codementor.io/angularjs/tutorial/angularjs-interview-questions-sample-answers –