1

I am very much new to AngularJS and I want to get values of OnChange of dropdownlist.

Below is my code which I tried

<div>
    <select ng-controller="UserSelection as User" ng-model="User.UserSelected" ng-change="onSelectChange()">            
        <option value="SAP_Executive">SAP Executive</option>
        <option value="Fiber_Engineer">Fiber Engineer</option>
        <option value="Fiber_Lead">Fiber Lead</option>
        <option value="CMM">CMM</option>
    </select>
</div>

Angular code

angular.module('App', [])
 .controller('UserSelection', function () {
  var User = this;      
  User.onSelectChange = function () {
      alert('change value selected');
  };
});

My alert is not firing. Please suggest what I am missing here

4
  • Try using ng-change="User.onSelectChange()" instead of ng-change="onSelectChange()" Commented Jan 31, 2019 at 11:13
  • @SudhirOjha: what is the difference in both the attributes with their values ?? Commented Jan 31, 2019 at 11:14
  • You have created an alias of your controller. Commented Jan 31, 2019 at 11:15
  • perfect..done thanks Commented Jan 31, 2019 at 11:17

1 Answer 1

1

Look here the following snippet. You have created an alias of your controller but not using it.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="App">
    <select ng-controller="UserSelection as User" ng-model="User.UserSelected" ng-change="User.onSelectChange()" ng-init="User.UserSelected='--select--'">
        <option value="--select--">--select--</option>            
        <option value="SAP_Executive">SAP Executive</option>
        <option value="Fiber_Engineer">Fiber Engineer</option>
        <option value="Fiber_Lead">Fiber Lead</option>
        <option value="CMM">CMM</option>
    </select>
</div>

<script>
 angular.module('App', [])
 .controller('UserSelection', function () {
  var User = this;      
  User.onSelectChange = function () {
      alert('change value selected');
  };
});
</script>

</body>
</html>

Sign up to request clarification or add additional context in comments.

7 Comments

how can I get default value "--Select--" on load ?
why blank value is coming, need to remove that ?
Which value you want default selected ?
I want --Select-- as default
@BN done you can use ng-selected . I have updated my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.