Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-change not working on ng-select

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!

like image 477
WebWanderer Avatar asked Aug 18 '14 19:08

WebWanderer


People also ask

What is ng-change in AngularJS?

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.

Does ng-change wait until all changes have been made?

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>.

Is @ng-select compatible with Angular 4?

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:

Does ng-change override the element's original onchange event?

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 Answers

2 things... both simple :)

  1. ng-change=onSelectChange should be onSelectChange()
  2. this.message should be this.scopeMessage
like image 98
Timothy Avatar answered Sep 18 '22 17:09

Timothy


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>
like image 44
Mike Haas Avatar answered Sep 16 '22 17:09

Mike Haas