Search code examples
htmlangularjsradio-buttonangular-ngmodelangular-ng-if

Show input field based on ng-if condition


I've two radio buttons VAT & CST and one input field. If I select VAT the input field should appear and if I select input field should be hidden. How to do it in angularJS. Thank You!

Here is my code

<input type="radio" ng-model="vat" name="source" value="VAT"> VAT
<input type="radio" name="source" value="CST"> CST

<input type="number" ng-if="vat" placeholder="Gunny VAT">

Solution

  • Use ng-model.

    <input type="radio" ng-model="flag" name="source" value="VAT"> VAT
    <input type="radio" ng-model="flag" name="source" value="CST"> CST
    
    <input type="number" ng-if="flag == 'VAT'" placeholder="Gunny VAT">
    

    FYI, you can also use ng-show ng-hide for the same.