javascript
  • angularjs
  • angularjs-directive
  • 2016-02-12 118 views 2 likes 
    2

    我有這個指令,使用服務「BoxService」採取一定的價值。刷新角度指令

    angular.module("App").directive('boxedWidget', 
        function() { 
         return { 
          restrict: 'E', 
          scope: {  
          'source': '=',  
          }, 
          replace:true, 
          templateUrl: '/widgets/box/widget.html', 
          link: function (scope, element, attrs) { 
           scope.item = scope.source; 
           boxService 
           .getBoxeValue(scope.item.id) 
           .success(function(data, status, headers, config) { 
            scope.item.value = data; 
           }); 
    
          } 
    
    
         } 
    
        }); 
    

    現在我想刷新每5秒的值。 有什麼建議嗎?

    回答

    2

    你可以嘗試$interval刷新你的數據在指令

    $interval(function() { 
    loadData(); 
    }, duration) 
    

    對於$interval更多的細節可以參考Angular $interval

    +1

    我剛剛嘗試'setInterval(scope.changeValue(),5000);' – user5917414

    +1

    它不一樣。 '$ interval'會導致刷新'範圍'。 'setInterval'不。 –

    +0

    是@ user5917414 ... Mosh Feu是對的這是非常基本但非常重要的區別。 – CandleCoder

    0

    你正確注入$間隔?

    angular.module("App").directive('boxedWidget', ['$interval', function($interval) { 
         return { 
          restrict: 'E', 
          scope: {  
          'source': '=',  
          }, 
          replace:true, 
          templateUrl: '/widgets/box/widget.html', 
          link: function (scope, element, attrs) { 
           scope.item = scope.source; 
           // don't forget to inject $interval in the directive declaration 
           $interval(function(){ 
            boxService 
            .getBoxeValue(scope.item.id) 
            .success(function(data, status, headers, config) { 
             scope.item.value = data; 
            }); 
           }, 5000); 
          } 
    
    
         } 
    
        }); 
    
    相關問題