admin管理员组

文章数量:1023758

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

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

本文标签: javascriptAngularJSWhy my watch is not workingStack Overflow