admin管理员组文章数量:1391000
I've got an ng-repeat
-ed table row, inside wich are a pair of angular select
s:
<div ng-controller="midiCtrl" id="midi-ctrl">
[...]
<tr ng-repeat="plugin in plugins">
<td><strong>{{plugin.name}}</strong></td>
<td>
<select class="span1" ng-model="selectedChannel" ng-options="item.ID as item.Title for item in channels">
</td>
<td>
<select class="span2" ng-model="selectedDevice" ng-options="item.ID as item.Title for item in devices">
</td>
</tr>
[...]
</div>
The controller is:
app.controller('midiCtrl', function ($scope, pluginDisplayedWindows) {
$scope.plugins = pluginDisplayedWindows.pluginsDisplayed; // An object
$scope.channels = [
{ID: 'all', Title: 'All'},
{ID: '0', Title: '1'},
{ID: '1', Title: '2'},
{ID: '2', Title: '3'}
];
$scope.devices = [
{ID: '0', Title: 'Device A'},
{ID: '1', Title: 'Device B'},
{ID: '2', Title: 'Device C'},
{ID: '3', Title: 'Device D'},
];
});
Now, I know that when one of the selects is selected, the corresponding object is set on the ng-model scope variable ($scope.selectedChannel
or $scope.selectedDevice
), but obviously it is set in the corresponding ng-repeat child scope.
How can I access the children scopes, in the controller? I want to save all the selections when the user presses a button, but if I try to do that in the midiCtrl
controller, I can't access to the children scopes created by ng-repeat
.
I've got an ng-repeat
-ed table row, inside wich are a pair of angular select
s:
<div ng-controller="midiCtrl" id="midi-ctrl">
[...]
<tr ng-repeat="plugin in plugins">
<td><strong>{{plugin.name}}</strong></td>
<td>
<select class="span1" ng-model="selectedChannel" ng-options="item.ID as item.Title for item in channels">
</td>
<td>
<select class="span2" ng-model="selectedDevice" ng-options="item.ID as item.Title for item in devices">
</td>
</tr>
[...]
</div>
The controller is:
app.controller('midiCtrl', function ($scope, pluginDisplayedWindows) {
$scope.plugins = pluginDisplayedWindows.pluginsDisplayed; // An object
$scope.channels = [
{ID: 'all', Title: 'All'},
{ID: '0', Title: '1'},
{ID: '1', Title: '2'},
{ID: '2', Title: '3'}
];
$scope.devices = [
{ID: '0', Title: 'Device A'},
{ID: '1', Title: 'Device B'},
{ID: '2', Title: 'Device C'},
{ID: '3', Title: 'Device D'},
];
});
Now, I know that when one of the selects is selected, the corresponding object is set on the ng-model scope variable ($scope.selectedChannel
or $scope.selectedDevice
), but obviously it is set in the corresponding ng-repeat child scope.
How can I access the children scopes, in the controller? I want to save all the selections when the user presses a button, but if I try to do that in the midiCtrl
controller, I can't access to the children scopes created by ng-repeat
.
2 Answers
Reset to default 3The simplest trick is to add the selected values to current plugin
object, so you can easily get the selected values and those values are bound to the correct plugin
object naturally. No other objects will be introduced. Very simple.
<select class="span1" ng-model="plugin.selectedChannel" ng-options="item.ID as item.Title for item in channels">
<select class="span2" ng-model="plugin.selectedDevice" ng-options="item.ID as item.Title for item in devices">
Working Demo 1
If you want store it separately, you can do
<select class="span1" ng-model="selected[$index].selectedChannel" ng-options="item.ID as item.Title for item in channels" />
<select class="span2" ng-model="selected[$index].selectedDevice" ng-options="item.ID as item.Title for item in devices" />
$scope.selected = [];
angular.forEach($scope.plugins, function (a) {
$scope.selected.push({
selectedChannel: undefined,
selectedDevice: undefined
});
})
Working Demo 2
You can define the selected object in the parent controller first. Objects you data-bind should always "have a dot" in them to avoid issues with prototypical inheritance, so you should bind to something like "selected.channel" rather than "selectedChannel"
app.controller('midiCtrl', function ($scope, pluginDisplayedWindows) {
$scope.plugins = pluginDisplayedWindows.pluginsDisplayed; // An object
$scope.selected = {};
$scope.selected.channel = {};
$scope.selected.device = {};
$scope.channels = [
{ID: 'all', Title: 'All'},
{ID: '0', Title: '1'},
{ID: '1', Title: '2'},
{ID: '2', Title: '3'}
];
$scope.devices = [
{ID: '0', Title: 'Device A'},
{ID: '1', Title: 'Device B'},
{ID: '2', Title: 'Device C'},
{ID: '3', Title: 'Device D'},
];
});
And update the HTML accordingly
<div ng-controller="midiCtrl" id="midi-ctrl">
[...]
<tr ng-repeat="plugin in plugins">
<td><strong>{{plugin.name}}</strong></td>
<td>
<select class="span1" ng-model="selected.channel" ng-options="item.ID as item.Title for item in channels">
</td>
<td>
<select class="span2" ng-model="selected.device" ng-options="item.ID as item.Title for item in devices">
</td>
</tr>
[...]
</div>
版权声明:本文标题:javascript - Angular select within a ng-repeat directive: how can I access the children scopes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744605149a2615307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论