无法访问ng-repeat中ng-options的选项

时间:2015-03-12 22:22:49

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat angularjs-ng-options

我希望能够在ng-repeat中的ng-options中访问一组选择选项,但ng-repeat会创建一个新的范围,我无法再访问它。有没有办法解决这个问题而不将其添加到重复数据中?

<body ng-controller="mainCtrl" class="container">
    <test repeater="repeater"></test>
</body>

模板:

<div ng-repeat="repeat in repeater">
    <span>{{repeat.type}}</span>
    <select ng-options="value for value in options">
         <option value="">Choose one:</option>
    </select>
</div>

JS:

angular.module('app').controller('mainCtrl', function($scope) {
  $scope.repeater = [
    {
      type: 'best'
    },
    {
      type: 'worst'
    },
    {
      type: 'ok'
    }
  ];
});

angular.module('app').directive('test', function() {
  return {
    scope: {
      repeater: '='
    },
    templateUrl: 'test.html',
    controller: function($scope) {
      $scope.options = ['Nope', 'Yup', 'Sure'];
    }
  };
});

Plunker

1 个答案:

答案 0 :(得分:3)

你关闭了,但遗漏了一件事。 ng-model

<select ng-model="selectedOption" ng-options="value for value in options">
     <option value="">Choose one:</option>
</select>