1

Below is my scenario.

I have 2 checkboxes named A & B which are inside the ng-repeat element. The possible states of these 2 checkboxes are

  1. When A is truthy, B can be either truthy or falsey
  2. When A is falsey, B can never be truthy.

Below is my code which I tried with ng-checked, but as ng-checked doesn't allow ng-model along with it, I couldn't come up with a solution.

NOTE: I need to capture both these 2 checkboxes state in my Model

<tbody>
   <tr ng-repeat="vehicle in editor.vehicles">                                        
      <td>
         <v-check name="A" ng-model="vehicle.modelA"></v-check>
      </td>
      <td>
         <v-check name="B" ng-model="vehicle.modelB" ng-checked="vehicle.modelA"></v-check>
      </td>
   </tr>
</tbody>

Above code uses a directive v-check which is nothing but a checkbox.

EDIT: The template of my v-check directive: <label class='checkbox-inline'><input ng-model='ngModel' type='checkbox'>{{text}}</label>

Thanks in advance.

2
  • Are you sure that your checkbox <input> tag is not wrapped around a <div> in your directive? In that case ng-checked might not work. Commented Sep 9, 2015 at 6:10
  • @ccg - Edited my question to include the template from the directive Commented Sep 9, 2015 at 6:16

2 Answers 2

3

Below is my solution to the problem, which also fixes the scenario as suggested by @entre in his comment.

<tbody>
 <tr ng-repeat="vehicle in editor.vehicles">                                        
  <td>
     <v-check name="A" ng-model="vehicle.modelA" ng-click="vehicle.modelB=false"></v-check>
  </td>
  <td>
     <v-check name="B" ng-model="vehicle.modelB" ng-disabled="!vehicle.modelA"></v-check>
  </td>
 </tr>
</tbody>
Sign up to request clarification or add additional context in comments.

Comments

1

use ng-disabled for B:

<tbody>
   <tr ng-repeat="vehicle in editor.vehicles">                                        
      <td>
         <v-check name="A" ng-model="vehicle.modelA"></v-check>
      </td>
      <td>
         <v-check name="B" ng-model="vehicle.modelB" ng-disabled="!vehicle.modelA"></v-check>
      </td>
   </tr>
</tbody>

I already tested this code:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example54-production</title>


  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>



</head>
<body ng-app="">
  <label>Click me to toggle: <input type="checkbox" ng-model="checked"></label><br/>
  <input type="checkbox" ng-model="button" ng-disabled="checked">
 </body>
</html>

1 Comment

It should be ng-disabled="!vehicle.modelA", right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.