WebWanderer
WebWanderer

Reputation: 10897

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!

Upvotes: 13

Views: 43072

Answers (2)

Mike Haas
Mike Haas

Reputation: 2563

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>

Upvotes: 6

Timothy
Timothy

Reputation: 1160

2 things... both simple :)

  1. ng-change=onSelectChange should be onSelectChange()
  2. this.message should be this.scopeMessage

Upvotes: 16

Related Questions