1

Can anyone explain why only the first $watch below is firing within the same controller? Both the inputs are text boxes with each ng-model assigned. (thanks in advance)

$scope.$watch('search', function() {
   if ($scope.watch !== ""){
       var filter = "{'name':{'$regex':'(?i).*"+$scope.search+".*'}}";
       fetch(filter);
      };
});


$scope.$watch('id', function() {
    if ($scope.id !== ""){
        var filter = "{'id':{'$regex':'(?i).*"+$scope.id+".*'}}";
        fetch2(filter);
    };
});
6
  • Looks OK to me. Have you tried console.log's within each watcher & condition statement? Commented Jul 4, 2017 at 13:16
  • You have $scope.watch in your if statement. Did you mean $scope.search ? Commented Jul 4, 2017 at 13:21
  • yeah console log shows nothing for it, but strangely I am finding it will fire (and work ok) if the earlier 'search' model has been populated. Both functions return to the same model but I presumed that should be fine. Commented Jul 4, 2017 at 13:24
  • Sergey, indeed I did, thanks. Hasn't made any difference re the latter firing but thank you. Commented Jul 4, 2017 at 13:25
  • Can you make a fiddle or smth like that? Commented Jul 4, 2017 at 13:27

4 Answers 4

1

Try this syntax and see what you will get in the console

$scope.$watch('id', function(newVal, oldVal) {
    console.log(oldVal, newVal);
});

if log is OK, why dont you try with the newVal like this

$scope.$watch('id', function(newVal, oldVal) {
    if (newVal !== ""){
        var filter = "{'id':{'$regex':'(?i).*"+newVal+".*'}}";
        fetch2(filter);
    };
});

Edited

Sign up to request clarification or add additional context in comments.

3 Comments

Yordan, it logs the original and new value in the console log as expected
Ok, so why dont you try with the newVal? I've edited my answer.
Begging your pardon, thanks to your advice re using newVal and oldVal, I was convince it was indeed firing and on checking my backend log I can see the http call being made successfully. So my issue must actually lie in my html rendering of the response. Many thanks for your guidance.
0
//Try this
var deregWatchGroup = $scope.$watchGroup(['1stmodelValue', '2ndmodelvalue'], function (newValues, oldValues) {
         console.log(newValues[0], oldValues[0]); //for 1st model value
         console.log(newValues[1], oldValues[1]); //for 2nd model value
         // here you can handle your stuff
 });
  //destroying watch
 $scope.$on('$destroy', deregWatchGroup);

Comments

0

To watch multiple scope in the same controller, you can use:

$scope.$watchGroup(['search', 'id'], function(newValues, oldValues) {

 newValues[0] -> $scope.search 
 newValues[1] -> $scope.id 

// Then perform your operations.

});

Comments

0

Thank you all. To answer, the issue was in my html rendering of the results and the root cause was my own idiocy. I has an extraneous ng-show which was hiding the results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.