Angular 为多个选定选项编写通用函数

问题描述 投票:0回答:1

您能否建议如何按事件选择两个选项的通用功能

$scope.selectionOpt = function($event){
            alert($event.target.value);
        }
    
<form>
    <select  ng-change="selectionOpt($event)">
      <option value=""></option>
      
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    
    <select  ng-change="selectionOpt($event)">
      <option value=""></option>
      
      <option value="asia">Asia</option>
      <option value="india">India</option>
      <option value="europe">Europe</option>
    </select>
  </form>

angular angularjs angularjs-directive
1个回答
0
投票

我们需要将

ng-model
ng-change
一起使用,这样角度就不会出现错误,我们只需传入
ng-model
字段并提醒已传递值!

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

function AppCtrl($scope, $http) {
  $scope.dropdown2 = null;
  $scope.dropdown1 = null;
  $scope.send = function() {
    console.log($scope);
  }

  $scope.selectionOpt = function(optionValue) {
    alert(optionValue);
  }
}
myApp.controller("AppCtrl", AppCtrl);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="AppCtrl">
    <form>
      <select ng-model="dropdown1" ng-change="selectionOpt(dropdown1)">
        <option value=""></option>

        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>

      <select ng-model="dropdown2" ng-change="selectionOpt(dropdown2)">
        <option value=""></option>

        <option value="asia">Asia</option>
        <option value="india">India</option>
        <option value="europe">Europe</option>
      </select>
    </form>
    <hr/>
    dropdown1: {{dropdown1}}<br/>
    dropdown2: {{dropdown2}}
  </div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.