My main problem is that the error is not pointing anywhere in my code and we dont have that many $apply
calls in our project. The code that seems to be generating the error:
我的主要问题是错误没有指向我的代码中的任何地方,我们的项目中没有那么多$ apply调用。似乎生成错误的代码:
<input type="text"
placeholder="{{ $root.pg.t('HEADER.CITY_OR_POSTAL') }}"
data-ng-focus="decideToStop();" data-ng-blur="decideToStart()"
data-ng-model="search.selected"
typeahead-on-select="search.locationQuery(geo_locations, search.selected)"
typeahead="location.name for location in getLocations($viewValue) | filter: $viewValue | limitTo:8" class="semi-bold">
In the controller:
在控制器中:
$scope.decideToStop = function(){
if($scope.actionTimeout){
$timeout.cancel($scope.actionTimeout);
$scope.actionTimeout = {};
}
$scope.MyService.CustomMethodThatDoesALotOfThings();
};
$scope.decideToStart = function(){
if(CustomCondition()){
$scope.actionTimeout = $timeout(function(){
$scope.MyService.AnotherCustomMethodThatDoesALotOfThings();
}, 150);
}
};
$root.pg
is just the polyglot.js library inside a service that is placed in the rootScope
for now. search
is the service corresponding to the location search.
$ root.pg只是现在放在rootScope中的服务中的polyglot.js库。 search是与位置搜索相对应的服务。
I am getting the error when I change the city in the input field. I have no idea how to debug this...
我在输入字段中更改城市时收到错误。我不知道如何调试这个......
1 个解决方案
#1
12
This is a problem with using ng-focus and other event directives at the same time. Most often, it happens when you use one of the event directives (ng-click, ng-blur) to focus an element that has ng-focus. It's actually a bug, but you can fix it easily. Best option is to write your own directive that applies the function async:
这是同时使用ng-focus和其他事件指令的问题。大多数情况下,当您使用其中一个事件指令(ng-click,ng-blur)来聚焦具有ng-focus的元素时,就会发生这种情况。它实际上是一个错误,但您可以轻松修复它。最好的选择是编写自己的应用函数async的指令:
app.directive('ngFocusAsync', ['$parse', function($parse) {
return {
compile: function($element, attr) {
var fn = $parse(attr.ngFocusAsync);
return function(scope, element) {
element.on('focus', function(event) {
scope.$evalAsync(function() {
fn(scope, {$event:event});
});
});
};
}
};
}]
)
If you don't want to do this, you can take the contents of the directive and apply it to your element manually, but since the directive is so small, the directive is preferable. When this gets fixed in core, you can then simply replace all your declarations.
如果您不想这样做,您可以获取该指令的内容并手动将其应用于您的元素,但由于该指令非常小,因此该指令更可取。当它在核心中得到修复时,您可以简单地替换所有声明。
See an example here: http://plnkr.co/edit/GBdvtN6ayo59017rLKPs?p=preview If your replace ng-focus-async with ng-focus, angular will throw when you click the button, or when you blur the second input field.
请参阅此处的示例:http://plnkr.co/edit/GBdvtN6ayo59017rLKPs?p = preview如果使用ng-focus替换ng-focus-async,则单击按钮时会出现角度,或者当您模糊第二个输入字段时。
#1
12
This is a problem with using ng-focus and other event directives at the same time. Most often, it happens when you use one of the event directives (ng-click, ng-blur) to focus an element that has ng-focus. It's actually a bug, but you can fix it easily. Best option is to write your own directive that applies the function async:
这是同时使用ng-focus和其他事件指令的问题。大多数情况下,当您使用其中一个事件指令(ng-click,ng-blur)来聚焦具有ng-focus的元素时,就会发生这种情况。它实际上是一个错误,但您可以轻松修复它。最好的选择是编写自己的应用函数async的指令:
app.directive('ngFocusAsync', ['$parse', function($parse) {
return {
compile: function($element, attr) {
var fn = $parse(attr.ngFocusAsync);
return function(scope, element) {
element.on('focus', function(event) {
scope.$evalAsync(function() {
fn(scope, {$event:event});
});
});
};
}
};
}]
)
If you don't want to do this, you can take the contents of the directive and apply it to your element manually, but since the directive is so small, the directive is preferable. When this gets fixed in core, you can then simply replace all your declarations.
如果您不想这样做,您可以获取该指令的内容并手动将其应用于您的元素,但由于该指令非常小,因此该指令更可取。当它在核心中得到修复时,您可以简单地替换所有声明。
See an example here: http://plnkr.co/edit/GBdvtN6ayo59017rLKPs?p=preview If your replace ng-focus-async with ng-focus, angular will throw when you click the button, or when you blur the second input field.
请参阅此处的示例:http://plnkr.co/edit/GBdvtN6ayo59017rLKPs?p = preview如果使用ng-focus替换ng-focus-async,则单击按钮时会出现角度,或者当您模糊第二个输入字段时。