I'm using a select box that is populated by the ng-options
. Unfortunately, I cannot get my ng-change
function to call.
Here is my Fiddle
Here is my js
:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.typeOptions = [
{ name: 'Feature', value: 'feature' },
{ name: 'Bug', value: 'bug' },
{ name: 'Enhancement', value: 'enhancement' }
];
$scope.scopeMessage = "default text";
var changeCount = 0;
$scope.onSelectChange = function()
{
changeCount++;
this.message = "Change detected: " + changeCount;
}.bind($scope);
//$scope.form = {type : $scope.typeOptions[0].value};
$scope.form = $scope.typeOptions[0].value;
}
And here is my HTML
:
<div ng-controller="MyCtrl" class="row">
<select ng-model='form' required ng-options='option.value as option.name for option in typeOptions' ng-change="onSelectChange"></select>
<div style="border:1px solid black; width: 100px; height:20px;">{{scopeMessage}}<div>
</div>
This is currently holding me up on my project for work, so any help will be geatly appreciated. Thanks!
AngularJS ng-change Event Directive In angularjs ng-change event is used to raise or call function whenever the value of input element changes. We can use this angularjs ng-change event with input elements like textboxes, checkboxes, dropdowns and textarea control elements.
It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript. Supported by <input>, <select>, and <textarea>.
This Angular post is compatible with Angular 4 upto latest versions, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11 & Angular 12 One of a package is @ng-select in our top list to convert traditional HTML select form control into an advanced selection component with many features:
The ng-change directive from AngularJS will not override the element's original onchange event, both the ng-change expression and the original onchange event will be executed.
2 things... both simple :)
ng-change=onSelectChange
should be onSelectChange()
this.message
should be this.scopeMessage
Your problem is that you are passing in the function reference to the ng-change directive. However, that directive expects an expression which can evaluated. So attach the parentheses to the function so that it can be evaluated as a function call.
Like here: http://jsfiddle.net/MTfRD/1101/
<select ng-model='form' required ng-options='option.value as option.name for option in typeOptions' ng-change="onSelectChange()"></select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With