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

I am trying to put a watch on a variable, so that if it's value changes I call the rest service an

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); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745590929a4634826.html

相关推荐

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

    I am trying to put a watch on a variable, so that if it's value changes I call the rest service an

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信