Jquery 使用ng repeat AngularJS中的ICheck单选按钮获取所选索引

Jquery 使用ng repeat AngularJS中的ICheck单选按钮获取所选索引,jquery,angularjs,icheck,Jquery,Angularjs,Icheck,$scope.schools是一个数组对象,如何获取输入[name=“masdoc school”]的选定索引以获取控制器中$scope.schools[selected index]的值。谢谢大家 问题解决!使用(wajatimur)的icheck指令 然后在ng中重复: app.directive('icheck', function($timeout, $parse) { return { require: 'ngModel', link: func

$scope.schools是一个数组对象,如何获取输入[name=“masdoc school”]的选定索引以获取控制器中$scope.schools[selected index]的值。谢谢大家

问题解决!使用(wajatimur)的icheck指令

然后在ng中重复:

app.directive('icheck', function($timeout, $parse) {
    return {
        require: 'ngModel',
        link: function($scope, element, $attrs, ngModel) {
            return $timeout(function() {
                var value;
                value = $attrs['value'];

                $scope.$watch($attrs['ngModel'], function(newValue) {
                    $(element).iCheck('update');
                })

                return $(element).iCheck({
                    checkboxClass: 'icheckbox_square-blue',
                    radioClass: 'iradio_square-blue'

                }).on('ifChanged', function(event) {
                    if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
                        $scope.$apply(function() {
                            return ngModel.$setViewValue(event.target.checked);
                        });
                    }
                    if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
                        return $scope.$apply(function() {
                            return ngModel.$setViewValue(value);
                        });
                    }
                });
            });
        }
    };
});

{{school.Location}
{{school.Type}
{{school.Name}

在ng模型中使用$parent,因为ngRepeat将为其子对象创建新的作用域(以及原型继承如何影响作用域),所以ng更改将只触发一次事件

$('input[type="radio"].flat-red').iCheck({
    checkboxClass: 'icheckbox_square-blue',
    radioClass: 'iradio_square-blue'
});
app.directive('icheck', function($timeout, $parse) {
    return {
        require: 'ngModel',
        link: function($scope, element, $attrs, ngModel) {
            return $timeout(function() {
                var value;
                value = $attrs['value'];

                $scope.$watch($attrs['ngModel'], function(newValue) {
                    $(element).iCheck('update');
                })

                return $(element).iCheck({
                    checkboxClass: 'icheckbox_square-blue',
                    radioClass: 'iradio_square-blue'

                }).on('ifChanged', function(event) {
                    if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
                        $scope.$apply(function() {
                            return ngModel.$setViewValue(event.target.checked);
                        });
                    }
                    if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
                        return $scope.$apply(function() {
                            return ngModel.$setViewValue(value);
                        });
                    }
                });
            });
        }
    };
});
<tbody>
   <tr ng-repeat="school in schools">
       <td>{{school.Location}}</td>
       <td>{{school.Type}}</td>
       <td>{{school.Name}}</td>
       <td class="school-action text-center">
          <label>
               <input icheck type="radio" name="iCheck" class="flat-red" ng-value={{$index}} ng-model="$parent.schoolIndex" ng-change="changeFaculty(schoolIndex)"/>
          </label>
       </td>
   </tr>