admin管理员组文章数量:1433902
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
2 Answers
Reset to default 5You 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
版权声明:本文标题:javascript - AngularJS : Why my watch is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745590928a2665192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论