如何重置$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的$ timeout內的$ scope?
- 2. AngularJS watch FileReader()。readyState change
- 3. AngularJS Clock Factory,$ timeout woes
- 4. 如何使用$ watch攻破Angularjs?
- 5. 如何在angularjs組件中使用$ watch
- 6. $ watch不叫,Angularjs
- 7. AngularJS $ scope。$ on()和$ timeout()
- 8. AngularJS - 觸發$ watch/$觀察者
- 9. AngularJS $ watch newValue未定義
- 10. 如何在AngularJS中停止$ timeout?
- 11. 如何禁用Android設置重置?
- 12. AngularJS - $ timeout裏面的請求
- 13. 如何在使用AngularJS的工廠中使用「$ timeout」?
- 14. AngularJS - $ timeout不是函數
- 15. ReferenceError $ timeout沒有定義angularjs
- 16. d3 timeout like angular .timeout
- 17. 使用$ watch兩個變量angularjs
- 18. AngularJS,更改動畫:指令$ watch不叫
- 19. 如何在AngularJS指令中使用$ http和$ timeout?
- 20. 如何停止/重寫Jquery TimeOut函數?
- 21. 如何在循環內設置Timeout
- 22. 如何重置範圍變量Angularjs
- 23. Angularjs $ timeout生成無限循環
- 24. 如何通過Android GPS使用Runnables獲取當前位置並搜索TimeOut?
- 25. AngularJS如何禁用按鈕後點擊
- 26. 如何禁用AngularJS輸入的修剪?
- 27. 如何使用gulp-watch?
- 28. $ timeout不會改變angularjs中的變量
- 29. Angularjs ng禁用始終保持禁用
- 30. $摘要沒有足夠的觸發$ watch - AngularJs
我明白了,謝謝。 – JackinC
答案的來源可能是 - https://www.codementor.io/angularjs/tutorial/angularjs-interview-questions-sample-answers –