【发布时间】:2016-06-23 20:07:11
【问题描述】:
我尝试使用来自 api 的数据更新 dom,但我得到“$watch 未定义”这是我的 ctrl
app.controller('counter', ['$scope', 'factory', function ($scope, factory) {
factory.get(function (res) {
$scope.proizvedeno = res;
$scope.output = $scope.proizvedeno.total_energy_output;
$scope.countTo = $scope.output;
$scope.countFrom = 0;
$scope.$watch("output", $scope.countTo)
console.log($watch);
$scope.reCount = function () {
$scope.countFrom = Math.ceil(Math.random() * 300);
$scope.countTo = Math.ceil(Math.random() * 7000) - Math.ceil(Math.random() * 600);
}; }]);
我尝试添加监视功能,以自动更新 dom,但这不起作用。有人可以帮我吗?
【问题讨论】:
-
删除console.log($watch);
-
您没有在任何可以记录的地方创建名为
$watch的变量。我建议您阅读更多关于 Angular 手表是什么以及如何操作的内容。 -
我从docs.angularjs.org/api/ng/type/$rootScope.Scope 阅读,但我看不到 var $watch 声明的位置。你能帮帮我吗? Thnx @thalaivar 也是
-
$watch 是 $scope 或 $rootScope 的方法。没有这样的变量。
-
谢谢你,我添加了 var scope = $rootScope;并声明 scope.$watch("output", $scope.countTo);还是不行