简体   繁体   English

角度,获取ng-repeat中已选中复选框(或单选)的索引

[英]Angular, getting index of checked checkbox (or radio) in ng-repeat

I am creating a set of forms with ng-repeat, each form has a checkbox(might change to radio if that matters) and I am trying to manipulate the specific level that is checked. 我正在创建一组带有ng-repeat的表单,每个表单都有一个复选框(如果重要的话,可以更改为radio),并且我试图操纵所检查的特定级别。 So what I am trying to do is pass the index of the checkbox (the index of itself within the ng-repeat. Let me show you what I mean 所以我想做的是通过复选框的索引(在ng-repeat中本身的索引。让我告诉你我的意思

<div class="saInstrcutionTableRow"  ng-repeat="parent in listTable track by $index">
                        <div class="saInstrcutionLeft"></div>
                        <!-- parent levels -->
                        <div  class="saInstrcutionCRight saInstrcutionTitle"><div class="parentSub1"> <input type="checkbox" ng-model="levelPicker">{{parent.name}}</div></div>

                        </div>

                    </div>

So here I'm just repeating the name with the input under the ng-model="levelPicker". 所以在这里,我只是用ng-model =“ levelPicker”下的输入重复该名称。 I am pushing a button outside of this which uses a function that I've set up that would only need to pass the checkbox's index within the repeat so it's like this - 我在此按钮之外按了一个按钮,该按钮使用了我已经设置的功能,只需要在重复中传递复选框的索引即可,就像这样-

<button type="button" class="resultsButton" ng-click="submitNewSub(Checkbox index here)">Submit</button>

Is there some way in angular to target the checked checkbox and get its index within the repeat? 是否有某种方式可以定向到选中的复选框并在重复中获得其索引? I am using this to add children within it. 我正在使用它在其中添加子代。 I've tried a few things but i'm not sure how to refer to it directly and get it's index (within angular). 我已经尝试了一些方法,但是我不确定如何直接引用它并获取它的索引(在角度范围内)。 Any input would be much appreciated. 任何输入将不胜感激。 Thanks!! 谢谢!!

You could capture the selected index withsomething like this 您可以像这样捕获选定的索引

<input type="checkbox" ng-change="onChange({{$index}})" ... />

onChange function onChange功能

  $rootScope.onChange = function(idx) {
    $rootScope.selectedIndex = idx;
  };

Then use selectedIndex for whatever you need. 然后根据需要使用selectedIndex Here's a quick example 这是一个简单的例子

ng-repeat creates a new scope for each item, so the parent can't directly access the 'levelPicker' set by any of the checkBoxes. ng-repeat为每个项目创建一个新作用域,因此父级无法直接访问任何复选框所设置的“ levelPicker”。

Most correct way is probably something like this: 最正确的方法可能是这样的:

$scope.listTable = [
    { name : 'a', selected: false },
    { name : 'b', selected: false },
    { name : 'c', selected: false }
];
$scope.selectMe = function ( index, previousValue ) {
    $scope.listTable[index].selected = !previousValue;
};
$scope.submitNewSub = function () {
    for ( var i=0; i<$scope.listTable.length; i++ ) {
        console.log($scope.listTable[i].name+
            ($scope.listTable[i].selected ?
                ' is selected' :
                ' is not selected')
        );
    }
}

With this in the html: 与此在HTML:

<input type="checkbox" ng-click="selectMe($index, levelPicker)" ng-model="levelPicker">{{parent.name}}

Or you can look at all the child scopes and check their levelPicker values: 或者,您可以查看所有子作用域并检查其levelPicker值:

$scope.submitNewSub = function () {
    for(var cs = $scope.$$childHead; cs; cs = cs.$$nextSibling) {
        console.log($scope.listTable[cs.$index].name+
            (cs.levelPicker ?
                ' is selected' :
                ' is not selected')
        );
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM