【问题标题】:Angular.js select looses focues option when $scope.$apply()$scope.$apply() 时 Angularjs 选择失去焦点选项
【发布时间】:2014-08-01 06:12:04
【问题描述】:

我有一个非常简单的选择元素:

<select id="selectCurrency" selectmodel="currencyPair" class="select"
        ng-model="currencyPair" ng-options="ft.id as ft.name for ft in contentChart"
        ng-change="currencyPairChange(currencyPair)">
</select>

当我下拉选择时,聚焦除第一个选项之外的任何选项,然后控制器调用 $scope.$apply(),焦点将自动返回到第一个选项。

我不知道如何避免这种行为。 任何帮助都非常感谢。

【问题讨论】:

  • 你能创建一个fiddle 并复制行为吗
  • 我会试一试的。
  • 如果您删除 $scope.$apply() 焦点停留在您选择的内容上,但问题是何时调用 $scope.$apply()。我在我的用例中需要它,所以你不能删除它。
  • 即使使用your fiddle,我也没有发现任何问题。它停留在它应该呆的地方。你确定错误在小提琴中被复制了吗

标签: html angularjs


【解决方案1】:

搞定了。我明白了from this question 的想法。这里是the fiddle

<div ng-app>
<div ng-controller="SelectCtrl">
    {{rate}}
    <select id="selectRate" ng-model="rate" data-ng-click="suspendDigest()">
     <option ng-repeat="ft in rateChart" value="{{ft.id}}">{{ft.name}}</option>
    </select>
</div>

JS:

function SelectCtrl($scope,$interval) {
 //$scope.rate='S';
 $scope.rateChart = [
   {id: 'S', name: 'buy rate'},
   {id: 'K', name: 'sell rate'},
   {id: 'R', name: 'average rate'}
];

setInterval(function(){        
   $scope.$apply();
}, 2000);
}

【讨论】:

  • 您的解决方案解决了问题。非常感谢。
猜你喜欢
  • 2012-08-03
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
相关资源
最近更新 更多