javascript - Is there a way to trigger callback function just once in $watchGroup | Angularjs? -


i'm watching 2 values in angular project that: $scope.$watchgroup( ['currentpage', 'newstatus'], $scope.setpage ).

and when both values changing function $scope.setpage executes 2 times accordingly, should once. how implement this?

my code:

js

app.controller('categorylistctrl', ['$scope', '$http', '$location', '$route', function($scope, $http, $location, $route) {     $scope.numperpage = 5;     $scope.currentpage = 1;     $scope.newstatus = -1;     var currentpagechanged = false;     var newstatuschanged = false;      function setpage() {         // function's code based on requests db     };     setpage();      // pagination based on db requests     $scope.$watchgroup( ['currentpage', 'newstatus'], function(newvalues, oldvalues) {         if ((newvalues[0] != oldvalues[0]) && !newstatuschanged) {             console.log(1);             // currentpage has changed             setpage();             currentpagechanged = true;         } else if ((newvalues[1] != oldvalues[1]) && !currentpagechanged) {             console.log(2);             // newstatus has changed             newstatuschanged = true;         } else {             console.log(3);             newstatuschanged = false;             currentpagechanged = false;         }     });     ... 

html

... <th>     <select class="form-control"         ng-model="ispublic"         ng-options="ispublic.name ispublic in ispublicscope"         ng-change="newstatus = ispublic.status; currentpage = 1;">         <option value="">show all</option>     </select> </th> <tr ng-repeat="category in categories | filter:search"> </tr> ... 

you can add flag, changes when both values change first time (only once). after can disable watch.

here example: jsbin example

can written this: editted

in example, 2 textboxes given, when both textbox values changed first time, watch increased count 1. after that, watch disabled , hence count not increase when changes occur in textboxes.

with slight change in code, may able want example.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -