ng-options不适用于基本情况

时间:2014-04-04 09:24:50

标签: javascript angularjs angularjs-ng-options

我试图通过依靠ng-options来构建一个非常简单的选择。

    angular.module('foo', []).directive('listSelect', ['$injector', function(injector){
  return {
    restrict: 'E',
    template: "<select ng-options='val as val for val in list'></select>",
    link: function(scope, elem, attrs){
      scope.list = ['foo', 'bar', 'bazz']; 
    }
  }
}]);

看似直截了当的指令未能提供正确的下拉选项。为什么呢?

plunk:http://plnkr.co/edit/PDmtUn2zBs2329PkHYiF?p=info

1 个答案:

答案 0 :(得分:3)

您的选择缺少模型。

template: "<select ng-model='hier' ng-options='val for val in list'></select>"

link: function (scope) {
  scope.list = ['foo', 'bar', 'bazz'];
  scope.hier = scope.list[0];
}