2016-06-21 14 views
2

を見る$で作業していないが、私はここで私は、に自動にし、時計の機能を追加しようとする私のCTRL更新DOMは、私は、APIからのデータとDOMを更新しよう

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を更新しますが、これは機能しません。誰かが私を助けることができますpls。

+0

console.log($ watch)を削除してください。 – Thalaivar

+0

あなたは、どこにでも記録できる '$ watch'という変数を作成していません。私は角膜時計が何で、どのように動作しているのかをもっと知ることをお勧めします。 – sdgluck

+0

私はhttps://docs.angularjs.org/api/ng/type/$rootScope.Scopeから読みましたが、var $ watchがどこに宣言されているのかわかりません。私plsヘル私はできますか? Thnx @thalaivar too – Arter

答えて

0

私は答えとしてこれを入れさせていただきますので、clarityを質問に追加してください。

あなたの実際のコード:1を改革

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

やあみんな、私は知らないこの最善の解決策が、ため、この作業であります私。すべて

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();  
}]); 

へのthnxは、Uは、タイマーをキャンセルしなければならないか、変更ルートタイマーがまだ働いた後、:)私のような問題を抱えていると、URLからデータをリロードします。みんなにThnx

関連する問題