Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng repeat指令中的角度选择:如何访问子作用域?_Javascript_Angularjs_Angularjs Scope_Angularjs Ng Repeat_Angularjs Select2 - Fatal编程技术网

Javascript ng repeat指令中的角度选择:如何访问子作用域?

Javascript ng repeat指令中的角度选择:如何访问子作用域?,javascript,angularjs,angularjs-scope,angularjs-ng-repeat,angularjs-select2,Javascript,Angularjs,Angularjs Scope,Angularjs Ng Repeat,Angularjs Select2,我有一个ng repeat-ed表格行,里面有一对角度selects: <div ng-controller="midiCtrl" id="midi-ctrl"> [...] <tr ng-repeat="plugin in plugins"> <td><strong>{{plugin.name}}</strong></td>

我有一个
ng repeat
-ed表格行,里面有一对角度
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>
现在,我知道当选择其中一个选项时,相应的对象在ng模型范围变量(
$scope.selectedChannel
$scope.selectedDevice
)上设置,但显然它在相应的ng重复子范围中设置


如何访问控制器中的子作用域?我想在用户按下按钮时保存所有选择,但如果我尝试在
midctrl
控制器中保存,我无法访问由
ng repeat

创建的子作用域。您可以先在父控制器中定义所选对象。数据绑定的对象中应该始终“有点”,以避免原型继承的问题,因此应该绑定到“selected.channel”之类的对象,而不是“selectedChannel”

并相应地更新HTML

   <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>

[...]
{{plugin.name}}
[...]

最简单的技巧是将选定的值添加到当前的
插件
对象中,这样您就可以轻松获得选定的值,并且这些值自然地绑定到正确的
插件
对象。不会引入其他对象。很简单

<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">

如果你想分开存放,你可以这样做

<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
    });
})

$scope.selected=[];
angular.forEach($scope.plugins,函数(a){
$scope.selected.push({
selectedChannel:未定义,
所选设备:未定义
});
})

真是太棒了。它之所以有效,是因为子对象继承了父作用域,不是吗?@janesconference以及当前的
插件
对象都是通过引用跟踪的。因此,像从子范围向其添加新属性这样的更改将被保留。出于好奇,如果我想使用另一个对象而不“污染”插件对象呢?哇。我不知道我可以访问模型中的索引。谢谢,非常有启发性。这不起作用。当一个选择被设置时,另一个也被设置。
<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">
<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
    });
})