最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - AngularJS : Why my watch is not working - Stack Overflow

matteradmin7PV0评论

I am trying to put a watch on a variable, so that if it's value changes I call the rest service and get updated count.

Here is how my code looks like

function myController($scope, $http) {

    $scope.abc = abcValueFromOutsideOfMyController;    

    $scope.getAbcCnt= function()
    {
        url2 = baseURL + '/count/' + $scope.abc;

        $http.get(url2).success(function (data) {
            $scope.abcCnt = data.trim();
        });
    };

    $scope.$watch('abc',getAbcCnt);
}

But, I get following error

ReferenceError: getAbcCnt is not defined

I am new to AngularJS, let me know if there is some fundamental concept I am missing and above is not possible to do.

This answer didn't help me AngularJS : Basic $watch not working

I am trying to put a watch on a variable, so that if it's value changes I call the rest service and get updated count.

Here is how my code looks like

function myController($scope, $http) {

    $scope.abc = abcValueFromOutsideOfMyController;    

    $scope.getAbcCnt= function()
    {
        url2 = baseURL + '/count/' + $scope.abc;

        $http.get(url2).success(function (data) {
            $scope.abcCnt = data.trim();
        });
    };

    $scope.$watch('abc',getAbcCnt);
}

But, I get following error

ReferenceError: getAbcCnt is not defined

I am new to AngularJS, let me know if there is some fundamental concept I am missing and above is not possible to do.

This answer didn't help me AngularJS : Basic $watch not working

Share Improve this question edited May 23, 2017 at 11:58 CommunityBot 11 silver badge asked May 10, 2013 at 16:19 WattWatt 3,16414 gold badges57 silver badges89 bronze badges 4
  • 1 do you mean $scope.$watch('abc',$scope.getAbcCnt); – Ruan Mendes Commented May 10, 2013 at 16:21
  • You've just asked a second, different question. Each post should be about a single question, not a whole problem. Suggestion: ask a new question and link to this. That makes the questions more useful and searchable to others. – Ruan Mendes Commented May 10, 2013 at 16:40
  • Ok, I will move it in a separate one. – Watt Commented May 10, 2013 at 16:40
  • I moved it to a separate post stackoverflow./questions/16487102/… – Watt Commented May 10, 2013 at 16:46
Add a ment  | 

2 Answers 2

Reset to default 5

You need to reference it in the $scope.

$scope.$watch('abc', $scope.getAbcCnt);

If you were to declare your function without the $scope prefix your existing call would work, but you would not be able to access the function from the view. If you don't need to access the function from the view, you can declare the function without the $scope and keep your existing $watch statement.

I think you meant

$scope.$watch('abc',$scope.getAbcCnt); 
Post a comment

comment list (0)

  1. No comments so far