2016-06-21 73 views
2

我嘗試更新與API數據DOM,但我得到「$表沒有定義」這是我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,但這不起作用。有人可以幫我請。

+0

刪除console.log($ watch); – Thalaivar

+0

您不會在任何可以記錄的地方創建名爲'$ watch'的變量。我建議你閱讀更多關於Angular手錶以及如何操作的信息。 – sdgluck

+0

我從https://docs.angularjs.org/api/ng/type/$rootScope.Scope中讀到,我看不到Var $ watch在哪裏聲明。你可以幫我嗎? Thnx @thalaivar – Arter

回答

0

讓我把這個作爲答案,所以它帶來更多clarity的問題。

您的實際代碼

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) //Changed this 
     console.log($watch); //removed this line 

     $scope.reCount = function() { 
      $scope.countFrom = Math.ceil(Math.random() * 300); 
      $scope.countTo = Math.ceil(Math.random() * 7000) - Math.ceil(Math.random() * 600); 
     }; }]); 

改革之一:

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", 
       function handleChange(newValue, oldValue) { 
        console.log("$scope.output:", newValue); 
     }) 

     $scope.reCount = function() { 
      $scope.countFrom = Math.ceil(Math.random() * 300); 
      $scope.countTo = Math.ceil(Math.random() * 7000) - Math.ceil(Math.random() * 600); 
     }; }]); 
+1

Thanke你,我會嘗試當我回家現在即時通訊離開辦公室 – Arter

+0

嗨,我用這個,但有在向數據庫添加新數據後,dom沒有任何變化。只有刷新後...我想要實時數據。 Thnx – Arter

+0

嘿@Thalaivar你在嗎? – Arter

0

嗨,大家好我解決這個問題與超時,我不知道這是最好的解決方案,但這個工作我。日Thnx所有

app.controller('counter', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) { 
$scope.reload = function() {    //add new function for timer 
$http.get(serviceBase + 'live-stats'). 
    success(function (res) { 
     $scope.proizvedeno = res; 

      $scope.wireless_charging_count = $scope.proizvedeno.wireless_charging_count; 
      $scope.countTo3 = $scope.wireless_charging_count; 
      $scope.countFrom3 = 0; 
      $scope.reCount = function() { 
      $scope.countFrom3 = Math.ceil(Math.random() * 300); 
        $scope.countTo3 = Math.ceil(Math.random() * 7000) - Math.ceil(Math.random() * 600); 
      }; 

     }); 
     var reload = $timeout(function(){   //set here $timeout 
      $scope.reload(); 
      }, 5000); 
      $scope.$on('$destroy', function(){  //must cancel $timeout 
$timeout.cancel(reload); 
}); 
    }; 
    $scope.reload();  
}]); 

ü必須取消計時器,否則你將有問題我一樣:),改變後的路線計時器仍在工作,並從URL重新加載數據。 thnx給大家