Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is Angular.js calling my function so frequently with ng-change and how do I make it call only once per change?
    primarykey
    data
    text
    <p>I'm building my first proper angular.js app after going through many tutorials. I've hit a problem with the <code>ngChange</code> directive. I'm trying to use it to call a function each time the value of a drop down list is changed by the user. I find that it calls the function multiple times upon page load and also multiple times each time the option is selected.</p> <p>I've put together <a href="http://jsfiddle.net/jeshuamaxey/2mXJY/" rel="nofollow">this jsfiddle</a> which demonstrates the problem I'm having.</p> <p>I'd like to know why it's exhibiting this behaviour and how I can achieve my desired outcome of one function call per option change and no calling of <code>change()</code> on page load. (this second criterion is less critical for my app, but I would like to know how to suppress this behaviour nonetheless).</p> <p>I've reproduced the code below for those of you who may be able to find an immediate error.</p> <h3>HTML</h3> <pre><code>&lt;body ng-app ng-controller="Controller"&gt; &lt;div&gt; &lt;h2&gt;Number of changes: {{numOfChanges}}&lt;/h2&gt; &lt;select ng-change="{{change()}}" ng-model="currentSelection"&gt; &lt;option ng-repeat="option in options"&gt;{{option}}&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <h3>JavaScript</h3> <pre><code>Controller = function($scope) { $scope.numOfChanges = 0; $scope.change = function() { $scope.numOfChanges++; } $scope.options = ["do", "ray", "me", "far", "so", "la", "tee","dah"] } </code></pre> <p>I'm aware this could well be due to my improper use/understanding of the angular methodology. I'd appreciate answers that address all the failings of this small example.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload