2014-03-27 13 views
4

我的主要问题是错误没有指向我的代码中的任何位置,我们没有那么多的$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"> 

在控制器:

$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仅仅是被放置在rootScope现在服务内的polyglot.js库。 search是与位置搜索对应的服务。

我在输入字段中更改城市时出现错误。我不知道如何调试这...

+0

ngFocus和ngBlur在一起玩的不好。当你删除其中一个时会发生什么? – Narretz

+0

我删除了ng-focus并解决了问题。但是...我需要两个! –

回答

10

这是一个使用ng焦点和其他事件指令在同一时间的问题。通常,当您使用某个事件指令(ng-click,ng-blur)来聚焦具有ng-focus的元素时,会发生这种情况。这实际上是一个错误,但您可以轻松修复它。最好的办法是写自己的指令适用的功能异步:

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}); 
      }); 
     }); 
     }; 
    } 
    }; 
}] 

如果你不想这样做,你可以把指令的内容,并将其应用到你的元素手动,但由于该指令太小,该指令是可取的。当它在核心中得到修复时,您可以简单地替换所有的声明。

在这里看到一个例子:http://plnkr.co/edit/GBdvtN6ayo59017rLKPs?p=preview 如果您更换NG-焦点异步与NG对焦,角度会当您单击按钮抛出,或者如果模糊了第二个输入字段。

+0

它的工作原理!谢谢! –