AngularJS获取变量中选定下拉选项的值

时间:2016-06-13 07:49:16

标签: javascript jquery angularjs drop-down-menu tinymce

我有下拉选择菜单&想要在api的请求参数中发送下拉选择的值。我的代码是......

<select class="form-control" id = "SelectionInput_reason">
   <option name="careerType" value="type_1">Career</option>
   <option name="examType" value="type_2">Exams</option>
</select>

getValue = function() {
    var selectedValue = this.value;
    var selectedText = this.options[this.selectedIndex].getAttribute('name');
    alert(selectedValue); 
    alert(selectedText); 
} 
document.getElementById('SelectionInput_reason').addEventListener('change', getValue );

如果可能的话,请在angularJS中给出答案......

如何在变量中获取tinymceeditor的文本输入?

$scope.tinymceModel = 'Initial content';
        $scope.getContent = function() {
          console.log('Editor content:', $scope.tinymceModel);
        };
        $scope.setContent = function() {
          $scope.tinymceModel = 'Time: ' + (new Date());
        };
        $scope.tinymceOptions = {
          selector: 'textarea',
          //plugins: 'link image code',
          toolbar: ' bold italic | undo redo | alignleft aligncenter alignright | code'
        };

HTML is ..

<div class="form-group">
     <textarea ui-tinymce="tinymceOptions" id="jander" ng-model="tinymceModel" placeholder="Ask your question" class="form-control"></textarea>
 </div>

2 个答案:

答案 0 :(得分:1)

在您的选择下拉列表中绑定model。如下所示

<select class="form-control" id = "SelectionInput_reason" data-ng-model="inputReason">

在您的控制器中,您将获得选择的选项

console.log($scope.inputReason)

答案 1 :(得分:0)

以下是附有小提琴的示例代码

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

myApp.controller('MyCtrl', function($scope) {

  $scope.shelf = [{
    'name': 'Banana',
    'value': '$2'
  }, {
    'name': 'Apple',
    'value': '$8'
  }, {
    'name': 'Pineapple',
    'value': '$5'
  }, {
    'name': 'Blueberry',
    'value': '$3'
  }];

  $scope.cart = {
     'fruit': $scope.shelf[0]
  };
});
&#13;
<div ng-controller="MyCtrl">
  <div>
    Fruit List:
    <select ng-model="cart.fruit" ng-options="state as state.name for state in shelf"></select>
    <br/>
    <tt>Cost & Fruit selected: {{cart.fruit}}</tt>
  </div>

</div>
&#13;
&#13;
&#13;

小提琴链接:http://jsfiddle.net/Td2NZ/1867/

我在这种情况下的建议是尝试将所有输入选择保持为Js对象(如此代码中的$ scope.shelf)导致Angular为数据的刚性和健壮处理而构建,最终你可以加载那些服务器或json文件中的选项,根本不需要触摸HTML!

希望这有帮助!