2017-07-18 33 views
0

使用按钮来设置自动刷新我大约在角JS自动刷新功能位忧虑。我试图与超时工作concept.It页面是令人耳目一新的时间间隔是我设置。如何开/关angularjs

app.controller('buildCtrl', ['$timeout', function($timeout) { 
dataService.getAppData($scope.periodData).success(function(response) { 
     $scope.loading = false; 
     bc.serviceGridData = response.appdata; 
     $scope.data = response.appdata; 
     $timeout(function() { 
      dataService.getAppData($scope.periodData); 
      $state.reload(); 
     }, 20000) 
} 
}]); 

现在,我需要一个按钮启用和禁用(开/关)这$超时functionality.If它是在页面需要刷新,如果它是关闭的网页不应该refresh.The时间间隔需要明确。

能否请你帮我this.How一个按钮就可以工作,设置此开/关功能。 在此先感谢。

+0

你可以用'$ timeout.cancel()'你的关机按钮。 TI将取消相关任务与超时 –

+0

然后我需要有两个按钮用于打开和关闭,但我希望它有一个按钮来完成。 –

回答

0

var app = angular.module('app', [ ]); 
 

 
app.controller('MainCtrl', function($scope,$timeout) { 
 
    
 
    
 
$scope.StartTimeOut=false; 
 
    $scope.ON_Off=function(isOn) 
 
    { 
 
    
 
    if(isOn) 
 
    { 
 
     alert("true") 
 
    } 
 
    else 
 
    { 
 
     alert("false") 
 
    } 
 
    } 
 
    
 
    
 
});
<!doctype html> 
 
<html ng-app="app"> 
 
    <head> 
 
    
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
    
 
    </head> 
 
    <body> 
 

 
    <div ng-controller="MainCtrl"> 
 
    <input type="button" value="Click" ng-click=ON_Off(StartTimeOut=!StartTimeOut) /> 
 
    </div> 
 

 
    <script src="app.js"></script> 
 
    </body> 
 
</html>

您可以在功能ifelse部分编写代码超时。