显示/隐藏div取决于AngularJS中的下拉选择

时间:2014-11-21 04:33:28

标签: angularjs

我正在尝试显示div,具体取决于从下拉列表中选择的内容。例如,如果用户选择'现金'从列表中显示Cash div或者如果用户选择' Check'从列表中显示Check div

我已将样本放在一起,但其不完整,需要连线

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

此外,当用户更改选择时,我还想清除之前的选择,以便当用户返回上一个选择时,他们不应该看到。

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

//控制器:

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


function MyCtrl($scope) {

  $scope.changeme = function() {
    alert('here');
  }
}

1 个答案:

答案 0 :(得分:15)

你没有初始化angular app因为你错过了ng-app指令

这是使用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.