2016-11-15 24 views
1
<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 type="radio" name="masdoc-school" class="flat-red" /> 
    </label> 
</td> 
</tr> 

我已經定義I檢查獲取選定的索引以ng重複AngularJS使用I檢查單選按鈕

$('input[type="radio"].flat-red').iCheck({ 
    checkboxClass: 'icheckbox_square-blue', 
    radioClass: 'iradio_square-blue' 
}); 

$ scope.schools是一個數組對象,我怎麼能得到選擇的輸入指數[名稱= 「masdoc-school」]獲取控制器中$ scope.schools [selected index]的值。謝謝大家!

回答

1

問題解決!在https://github.com/fronteed/icheck/issues/62

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

使用(wajatimur)的I確認指令,然後在NG-重複:使用$父在NG-模型,因爲ngRepeat將爲其創造兒童的新領域(

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

以及原型繼承如何影響範圍),所以ng-change只會觸發一次事件。