无法在 ng-repeat 中访问 ng-options 的选项
Can't access options for ng-options inside an ng-repeat
我希望能够在 ng-repeat 中的 ng-options 中访问一组 select 选项,但是 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'];
}
};
});
你很接近,但遗漏了一件事。 ng-model
<select ng-model="selectedOption" ng-options="value for value in options">
<option value="">Choose one:</option>
</select>
我希望能够在 ng-repeat 中的 ng-options 中访问一组 select 选项,但是 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'];
}
};
});
你很接近,但遗漏了一件事。 ng-model
<select ng-model="selectedOption" ng-options="value for value in options">
<option value="">Choose one:</option>
</select>