使用静态值获取angularjs中ng-change的下拉值

时间:2016-11-23 11:06:29

标签: javascript angularjs

我希望在更改ng-change的下拉列表时获得价值。我的代码如下: -

<select class="form-control" id="cobSRQ2" ng-change="cobchange()" required>
    <option>[Select One]</option>
    <option value="Primary" selected>Primary</option>
    <option value="Secondary">Secondary</option>
    <option value="Tertiary">Tertiary</option>
</select>

我想将值发送给Angular JS控制器。

请帮忙

3 个答案:

答案 0 :(得分:2)

使用ng-model传递值:

 app.controller('controllername',function($scope){
 $scope.drpmodel=-1
 $scope.cobchange=function(){

     alert($scope.drpmodel);

  }


 });

 <select class="form-control" id="cobSRQ2" ng-model="drpmodel" ng-change="cobchange()" required>
   <option value="-1">[Select One]</option>
   <option value="Primary" selected>Primary</option>
   <option value="Secondary">Secondary</option>
   <option value="Tertiary">Tertiary</option>
</select>

答案 1 :(得分:1)

您可以向ng-model添加<select>属性,并将其传递到ng-change函数中,如:

<select ng-model="cob" class="form-control" id="cobSRQ2" ng-change="cobchange(cob)" required>
    ...
</select>

答案 2 :(得分:0)

要使ng-change工作,你必须提供ng-model来选择

<select class="form-control" id="cobSRQ2" ng-model="myModel" ng-change="cobchange()"    required>
<option>[Select One]</option>
<option value="Primary" selected>Primary</option>
<option value="Secondary">Secondary</option>
<option value="Tertiary">Tertiary</option>
</select>

控制器内部

$scope.cobchange=function(){
console.log($scope.myModel);
          }