javascript
  • angularjs
  • angularjs-directive
  • 2016-02-12 176 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); 
          } 
    
    
         } 
    
        }); 
    
    相关问题