Miranda Miranda - 1 month ago 8
AngularJS Question

bind a textbox value if checkbox is checked

how to pass a textbox value to controller based on condition .
if checkbox is checked then bind the textbox with object value and pass to the controller other wise just leave it blank and pass the user input to controller.. what i am doing is not working. what is wrong with my code it is working in the case if checkbox is checked.

$scope.Product = [
{"ProductID":12,"LNumber":"hrx",weght:"2"},
{"ProductID":13,"LNumber":"pty",weght:"1"}
]

<div>
<div>
<input type="checkbox" data-ng-model="Copyknotes" />
<span >Copy notes from</span>
</div>

<table data-ng-repeat="Item in Product track by $index">
<tr >
<td>
<input type="radio" name="groupName_{{Item.ProductID}}" data-ng-model ="Item.isSelected" />
</td>
<td data-ng-if="Copyknotes == true">
<input type="text" data-ng-model="Item.LNumber">
</td>
<td data-ng-if="Copyknotes == false" id="hi">
<input type="text" data-ng-model="Item.LNumber=""">
</td>
</tr>
</table>
</div>

Answer

Just use

data-ng-init=""

instead of

data-ng-model="Item.LNumber=""" 

use

data-ng-model="Item.LNumber"


<table data-ng-repeat="Item in Product track by $index">                                          
   <tr >
     <td>                                              
        <input type="radio" name="groupName_{{Item.ProductID}}"  data-ng-model ="Item.isSelected"  />
     </td>
     <td data-ng-if="Copyknotes == true">
       <input type="text" data-ng-model="Item.LNumber">
      </td>
      <td data-ng-if="Copyknotes == false" id="hi">
      <input type="text" data-ng-model="Item.LNumber" data-ng-init="">
       </td>
       </tr>
</table>