AngularJS:为什么我的手表是不工作 [英] AngularJS : Why my watch is not working

查看:113
本文介绍了AngularJS:为什么我的手表是不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把手表上的变量,因此,如果它的价值变化我称之为REST服务并获得更新计数。

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.

下面是我的code的样子

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

我是新来AngularJS,让我知道,如果有我缺少一些基本的概念,上面是不可能的事情。

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

这答案对我没有帮助
基本$腕表不是AngularJS

推荐答案

您需要引用它在 $范围

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

如果你申报你的函数没有 $范围 preFIX现有通话将工作,但您将无法从该视图访问功能。如果你不需要从视图访问的功能,你可以声明函数而不 $范围,并保持现有的 $观看语句。

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.

这篇关于AngularJS:为什么我的手表是不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆