Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/Hide divs depend on dropdown select in AngularJS

Tags:

angularjs

I am trying to show a div depending on what is selected from the drop down list. For example, if a user selects 'Cash' from the list show Cash div or if the user select 'Check' from the list show Check div

I have put together sample but its incomplete and needs to wire-up

http://jsfiddle.net/abuhamzah/nq1eyj1v/

Also when the user change the selection I would also like to clear the previous selection so when the user goes back to the previous selection they should not see.

<div ng-controller="MyCtrl">
   <div class="col-xs-12">
      <label class="col-xs-6 control-label">Type:</label>
      <div class="col-xs-6">
         <select name="type" ng-model="payment.type" ng-dropdown required ng-change="changeme()" >
            <option ng-option value="Cash">Cash</option>
            <option ng-option value="Check">Check</option>
            <option ng-option value="Money Order">Money Order</option> 
         </select>
      </div>
   </div>
   <div class="col-xs-12" id="cash">
      <div >
         <label class="col-xs-6 control-label">Cash :</label>
         <div class="col-xs-6">
            <input type="number" class="form-control"  ng-model="payment.cash"    />
         </div>
      </div>
    </div>

    <div class="col-xs-12" id="check">
      <div >
         <label class="col-xs-6 control-label">check :</label>
         <div class="col-xs-6">
            <input type="number" class="form-control"  ng-model="payment.check"    />
         </div>
      </div>
    </div>

    <div class="col-xs-12" id="money_order">
      <div >
         <label class="col-xs-6 control-label">money_order :</label>
         <div class="col-xs-6">
            <input type="number" class="form-control"  ng-model="payment.money_order"    />
         </div>
      </div>
    </div>

</div>

//controller:

var myApp = angular.module('myApp',[]);


function MyCtrl($scope) {

  $scope.changeme = function() {
    alert('here');
  }
}
like image 467
Nick Kahn Avatar asked Nov 21 '14 04:11

Nick Kahn


1 Answers

you didnt initialize as the angular app coz u missed the ng-app directive first

and this is the solution using ng-if

DEMO

<div ng-app="myApp" ng-controller="MyCtrl">  // initialize as a angular app

<div class="col-xs-12" id="cash" ng-if="payment.type == 'Cash'"> // this div will show if the value of `payment.type` model equals to `cash`. and so on.
like image 181
Kalhan.Toress Avatar answered Sep 21 '22 12:09

Kalhan.Toress