Merge pull request #2253 from coreos-inc/BUG-angular-apply-error
fix(js/onresize): make sync call async in angular
This commit is contained in:
commit
72191bbd37
1 changed files with 9 additions and 5 deletions
|
@ -1,14 +1,18 @@
|
|||
/**
|
||||
* Adds an onresize event attribtue that gets invokved when the size of the window changes.
|
||||
* Adds an onresize event attribute that gets invokved when the size of the window changes.
|
||||
*/
|
||||
angular.module('quay').directive('onresize', function ($window, $parse) {
|
||||
angular.module('quay').directive('onresize', function ($window, $parse, $timeout) {
|
||||
return function (scope, element, attr) {
|
||||
var fn = $parse(attr.onresize);
|
||||
|
||||
var notifyResized = function() {
|
||||
scope.$apply(function () {
|
||||
// Angular.js enforces only one call to $apply can run at a time.
|
||||
// Use $timeout to make the scope update safe, even when called within another $apply block,
|
||||
// by scheduling it on the call stack.
|
||||
// See docs: https://docs.angularjs.org/error/$rootScope/inprog
|
||||
$timeout(function () {
|
||||
fn(scope);
|
||||
});
|
||||
}, 0);
|
||||
};
|
||||
|
||||
angular.element($window).on('resize', null, notifyResized);
|
||||
|
@ -17,4 +21,4 @@ angular.module('quay').directive('onresize', function ($window, $parse) {
|
|||
angular.element($window).off('resize', null, notifyResized);
|
||||
});
|
||||
};
|
||||
});
|
||||
});
|
||||
|
|
Reference in a new issue