最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call a function on change of a select option in angularjs - Stack Overflow

programmeradmin3浏览0评论

I have a dropdown (Select, option), and the options get populated dynamically using ng-repeat. I want to call a function within $scope on change of the option. Here is the HTML

<div class="type-style" ng-show="accountCount > 3">
     <select class="account-filter">
          <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]"></option>
     </select>
</div>

I know we can use ng-change, but ng-change needs ng-model and I don't think ng-model is needed in my case. Can anyone help?

As you can notice in the code, I am doing and ng-repeat on option tag. So will I get that 'account' which I have used in ng-repeat, in the select tag? So that I can use it in ng-model?

Also, each account is a string that has 3 values separated by a '|'. When I call a function on ng-change, I want to pass the 2nd value with it.

For eg: Each account has Name | number | number2. Now in ng-repeat I am just showing name, as you can see I have done a split in ng-bind. So if I use ng-model in select tag, I will just get name. But I want number to be passed as parameter in the function.

I have a dropdown (Select, option), and the options get populated dynamically using ng-repeat. I want to call a function within $scope on change of the option. Here is the HTML

<div class="type-style" ng-show="accountCount > 3">
     <select class="account-filter">
          <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]"></option>
     </select>
</div>

I know we can use ng-change, but ng-change needs ng-model and I don't think ng-model is needed in my case. Can anyone help?

As you can notice in the code, I am doing and ng-repeat on option tag. So will I get that 'account' which I have used in ng-repeat, in the select tag? So that I can use it in ng-model?

Also, each account is a string that has 3 values separated by a '|'. When I call a function on ng-change, I want to pass the 2nd value with it.

For eg: Each account has Name | number | number2. Now in ng-repeat I am just showing name, as you can see I have done a split in ng-bind. So if I use ng-model in select tag, I will just get name. But I want number to be passed as parameter in the function.

Share Improve this question edited Apr 11, 2016 at 6:16 Vinod Bhavnani asked Apr 11, 2016 at 5:11 Vinod BhavnaniVinod Bhavnani 2,2251 gold badge17 silver badges19 bronze badges 1
  • If you want to use value of your current selected option of dropdown in function then ng-model will help you. use ng-change along with ng-model. – Sudz Commented Apr 11, 2016 at 5:34
Add a ment  | 

4 Answers 4

Reset to default 7

There is no problem in adding a ng-model because that will help you to use its value. For example:

<select class="account-filter" ng-change="customerSelected()" ng-model="selectedCustomer">
      <option ng-repeat="account in getCustomerName() track by $index"
          ng-class="(account | limitTo: 18)"
          ng-bind="account.split('|')[0]"
          value="{{account}}"></option>
</select>

Now you can read the selected customer's value in your controller by simply $scope.selectedCustomer.

Update

After updating the question with more description, you have two option:

Option 1 (remended)

You can write like this (although, you can achieve much of it in the HTML itself but it is cleaner to do in the controller):

$scope.customerSelected = function() {
     var selectedCustomer = $scope.selectedCustomer;
     if (!selectedCustomer) {
          alert("Please select a customer");
          return;
     }

     var secondId = selectedCustomer.split('|')[2];
     // You can directly put your main logic here but since you want to
     // call a separate method with passing the id
     $scope.doSomethingWithCustomer(secondId);
};

$scope.doSomethingWithCustomer = function(id) {
    // Your main logic here after selecting the customer
};

and modify your HTML as I described above:

<select class="account-filter" ng-change="customerSelected()" ng-model="selectedCustomer">
      <option ng-repeat="account in getCustomerName() track by $index"
          ng-class="(account | limitTo: 18)"
          ng-bind="account.split('|')[0]"
          value="{{account}}"></option>
</select>

Option 2 (less cleaner)

$scope.doSomethingWithCustomer = function(id) {
    // Your main logic here after selecting the 2nd customer
};

and modify your HTML as:

<select class="account-filter" ng-model="selectedCustomer"
    ng-change="doSomethingWithCustomer(selectedCustomer.split('|')[2])">
      <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]" value="{{account}}"></option>
</select>

Option 3

If you want to achieve it without the ng-model and without jQuery then you can use a directive:

See a working example below.

var a = angular.module("sa", [])

a.controller("foobar", function($scope) {
  $scope.getCustomerName = function() {
    return ['Foo', 'Bar'];
  };

  $scope.customerSelected = function() {
    console.log("I'm changed");
    alert("I'm changed");
  };
});

a.directive("changeMe", function() {
  return {
    scope: {
      changeMe: '&'
    },
    link: function($scope, element, attr) {
      element.on("change", function() {
           $scope.changeMe();
      })
    }
  }
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="sa" ng-controller="foobar">
  <select class="account-filter" change-me="customerSelected()">
    <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]"></option>
  </select>
</div>

Just add a ng-change in select, ng-model is required for ng-change

<select class="account-filter" ng-change="fnName()" ng-model="account">
     <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]"></option>
</select>
<div class="type-style" ng-show="accountCount > 3">
     <select class="account-filter" id="detect_change">
          <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]"></option>
     </select>
</div>
<script>
$('#detect_change').change(function() {
//change detect function
}
</script>

You can use ng-change event

<script>
            
            var myapp=angular.module("my_app",[]);
            
            myapp.controller("my_ctrl",function($scope){
                
                
                $scope.your_function=function(s){
                
                alert(s);
            }
            
    });
            
</script>
<div ng-app="my_app" ng-controller="my_ctrl">

<div class="type-style" ng-show="accountCount > 3" >
     <select class="account-filter" ng-model="selected_model" ng-change="your_function({{selected_model}});">
          <option ng-repeat="account in getCustomerName() track by $index" ng-class="(account | limitTo: 18)" ng-bind="account.split('|')[0]"></option>
     </select>
</div>

</div>
发布评论

评论列表(0)

  1. 暂无评论