当控件获得焦点时,AngularJS加载选择选项

时间:2014-06-05 21:28:26

标签: angularjs

我想让我的页面重量轻一些。我有一些元素必须是选择选项,但我需要它们加载就像用户触摸控件。也许会在一瞬间显示一个加载对话框,以便给予反馈。我怎么能在Angular中做到这一点?我尝试了ng-click和ng-focus来触发模型选项,但它永远不会触发。

<select name="Category"  ng-model="selectedCategory" ng-options="item.name as item.name for item in     categories">
        <option value="" disabled selected>All Categories</option>
  </select>

1 个答案:

答案 0 :(得分:2)

ng-focus应该有效。只需让$scope.categories作为空数组开始,然后将其填充到ng-focus函数中:

$scope.categories = [];
$scope.selectedCategory = {};

$scope.loadOptions = function() {
  if ($scope.categories.length == 0) {
    $scope.categories = [{
      name: 'option1'
    }, {
      name: 'option2'
    }, {
      name: 'option3'
    }];
  }
}

HTML:

<select name="Category" ng-model="selectedCategory" 
  ng-options="item.name as item.name for item in categories" 
  ng-focus="loadOptions()">

Plunker